Selenium 无法将密钥发送到 youtube 搜索框

问题描述 投票:0回答:1

最近我开始使用 selenium 进行一些工作,并遇到了无法使用以下代码将密钥发送到 youtube 搜索框的问题:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
service = Service(executable_path="chromedriver.exe")
driver = webdriver.Chrome(service=service)
driver.get("https://youtube.com")
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "ytd-searchbox")))
input_element = driver.find_element (By.CLASS_NAME,"ytd-searchbox")
input_element.send_keys("some text"+Keys.ENTER)
time.sleep(100)

类的名称应该是正确的,因为当我运行该函数时,搜索框会突出显示:

input_element.click()

终端输出为:


DevTools listening on ws://127.0.0.1:61618/devtools/browser/a9e75182-8834-47ba-9b5d-94dad187f4d4
Traceback (most recent call last):
  File "c:\Users\noahm\Desktop\Vs_Code\programming\creativestuff\login_bot.py", line 13, in <module>
    input_element.send_keys("some text"+Keys.ENTER)
  File "C:\Users\noahm\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\selenium\webdriver\remote\webelement.py", line 231, in send_keys
    self._execute(
  File "C:\Users\noahm\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\selenium\webdriver\remote\webelement.py", line 395, in _execute
    return self._parent.execute(command, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\noahm\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\selenium\webdriver\remote\webdriver.py", line 354, in execute
    self.error_handler.check_response(response)
  File "C:\Users\noahm\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\selenium\webdriver\remote\errorhandler.py", line 229, in check_response  
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
  (Session info: chrome=126.0.6478.127)
Stacktrace:
        GetHandleVerifier [0x00007FF6B4ECEEA2+31554]
        (No symbol) [0x00007FF6B4E47ED9]
        (No symbol) [0x00007FF6B4D08559]
        (No symbol) [0x00007FF6B4D51D73]
        (No symbol) [0x00007FF6B4D4FEDB]
        (No symbol) [0x00007FF6B4D7D02A]
        (No symbol) [0x00007FF6B4D4BA76]
        (No symbol) [0x00007FF6B4D7D240]
        (No symbol) [0x00007FF6B4D9C977]
        (No symbol) [0x00007FF6B4D7CDD3]
        (No symbol) [0x00007FF6B4D4A33B]
        (No symbol) [0x00007FF6B4D4AED1]
        GetHandleVerifier [0x00007FF6B51D8B1D+3217341]
        GetHandleVerifier [0x00007FF6B5225AE3+3532675]
        GetHandleVerifier [0x00007FF6B521B0E0+3489152]
        GetHandleVerifier [0x00007FF6B4F7E776+750614]
        (No symbol) [0x00007FF6B4E5375F]
        (No symbol) [0x00007FF6B4E4EB14]
        (No symbol) [0x00007FF6B4E4ECA2]
        (No symbol) [0x00007FF6B4E3E16F]
        BaseThreadInitThunk [0x00007FFA88B1257D+29]
        RtlUserThreadStart [0x00007FFA8954AF28+40]



当我尝试该网站的代码时,出现了相同的输出: 文字

非常感谢您的帮助

python selenium-webdriver
1个回答
0
投票

看起来至少有 9 个节点随选择器返回

".ytd-searchbox"

(using chrome inspector)
> location.href
'https://www.youtube.com/'
> document.querySelector(".ytd-searchbox")
NodeList(9) [form#search-form.style-scope.ytd-searchbox, div#container.style-scope.ytd-searchbox, yt-icon#search-icon.style-scope.ytd-searchbox, input#search.ytd-searchbox, div#search-clear-button.style-scope.ytd-searchbox, ytd-button-renderer.style-scope.ytd-searchbox, button#search-icon-legacy.style-scope.ytd-searchbox, yt-icon.style-scope.ytd-searchbox, tp-yt-paper-tooltip.style-scope.ytd-searchbox]

通常,除非必要,否则使用

class
属性并不是一个好主意。

奇怪的是,就 YouTube 的搜索框而言,“ID”甚至不是唯一的 🤔

尝试在此 CSS 选择器上进行匹配:

...

input_element = driver.find_element (By.CSS_SELECTOR, "input#search")
input_element.send_keys("some text"+Keys.ENTER)

...
© www.soinside.com 2019 - 2024. All rights reserved.