开始编码,所以一切对我来说都是新的。一直试图让这个代码工作,但不断遇到错误。我尝试添加某种类型的延迟,因为我看到它可能是网站没有及时加载,但这不起作用。
代码:
import time
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('https://www.youtube.com/')
time. sleep(5)
searchbox = driver.find_element("xpath", '//*[@id="search"]')
time. sleep(5)
searchbox.send_keys('Ludwig')
time. sleep(5)
searchButton = driver.find_element("xpath", '//*[@id="search-icon-legacy"]')
searchButton.click()
错误:
[WDM] - ====== WebDriver manager ======
[WDM] - Current google-chrome version is 103.0.5060
[WDM] - Get LATEST chromedriver version for 103.0.5060 google-chrome
[WDM] - Driver [C:Usersregra.wdmdriverschromedriverwin32103.0.5060.53chromedriver.exe] found in cache
Warning (from warnings module):
File "C:UsersregraDownloadsWebAutomationPython-masterWebAutomationPython-masterautomation.py", line 5
driver = webdriver.Chrome(ChromeDriverManager().install())
DeprecationWarning: executable_path has been deprecated, please pass in a Service object
Traceback (most recent call last):
File "C:UsersregraDownloadsWebAutomationPython-masterWebAutomationPython-masterautomation.py", line 10, in <module>
searchbox.send_keys('Ludwig')
File "C:UsersregraAppDataLocalProgramsPythonPython39libsite-packagesseleniumwebdriverremotewebelement.py", line 223, in send_keys
self._execute(Command.SEND_KEYS_TO_ELEMENT,
File "C:UsersregraAppDataLocalProgramsPythonPython39libsite-packagesseleniumwebdriverremotewebelement.py", line 396, in _execute
return self._parent.execute(command, params)
File "C:UsersregraAppDataLocalProgramsPythonPython39libsite-packagesseleniumwebdriverremotewebdriver.py", line 435, in execute
self.error_handler.check_response(response)
File "C:UsersregraAppDataLocalProgramsPythonPython39libsite-packagesseleniumwebdriverremoteerrorhandler.py", line 247, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
(Session info: chrome=103.0.5060.66)
Stacktrace:
Backtrace:
Ordinal0 [0x00FE6463+2188387]
Ordinal0 [0x00F7E461+1762401]
Ordinal0 [0x00E93C40+801856]
Ordinal0 [0x00EBCF63+970595]
Ordinal0 [0x00EBC63E+968254]
Ordinal0 [0x00EDC7DC+1099740]
Ordinal0 [0x00EB7FF4+950260]
Ordinal0 [0x00EDC9F4+1100276]
Ordinal0 [0x00EECC22+1166370]
Ordinal0 [0x00EDC5F6+1099254]
Ordinal0 [0x00EB6BE0+945120]
Ordinal0 [0x00EB7AD6+948950]
GetHandleVerifier [0x012871F2+2712546]
GetHandleVerifier [0x0127886D+2652765]
GetHandleVerifier [0x0107002A+520730]
GetHandleVerifier [0x0106EE06+516086]
Ordinal0 [0x00F8468B+1787531]
Ordinal0 [0x00F88E88+1805960]
Ordinal0 [0x00F88F75+1806197]
Ordinal0 [0x00F91DF1+1842673]
BaseThreadInitThunk [0x76FFFA29+25]
RtlGetAppContainerNamedObjectPath [0x771E7A9E+286]
RtlGetAppContainerNamedObjectPath [0x771E7A6E+238]
任何帮助都将是非常感激的
有5个元素匹配你的定位器,和webdriver试图发送密钥到第一个实际上是不可交互的。修复定位器,替换:
searchbox = driver.find_element("xpath", '//*[@id="search"]')
searchbox = driver.find_element("xpath", '//input[@id="search"]')