sta*_*-07 10 python exception selenium-chromedriver selenium-webdriver
尝试使用 Selenium Webdriver 创建对象时出现以下错误。
"\selenium\webdriver\common\driver_finder.py", line 42, in get_path
path = SeleniumManager().driver_location(options) if path is None else path
"\selenium\webdriver\common\selenium_manager.py", line 74, in driver_location
browser = options.capabilities["browserName"]
AttributeError: 'str' object has no attribute 'capabilities'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
"\selenium_webdriver_webscraping.py", line 4, in <module>
driver = webdriver.Chrome(chrome_driver_path)
"\selenium\webdriver\chrome\webdriver.py", line 47, in __init__
self.service.path = DriverFinder.get_path(self.service, self.options)
"\selenium\webdriver\common\driver_finder.py", line 44, in get_path
raise NoSuchDriverException(f"Unable to obtain {service.path} using Selenium Manager; {err}")
selenium.common.exceptions.NoSuchDriverException: Message: Unable to obtain chromedriver using Selenium Manager; 'str' object has no attribute 'capabilities'; For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors/driver_location
Run Code Online (Sandbox Code Playgroud)
这是我使用的代码:
from selenium import webdriver
chrome_driver_path = <chrome drive .exe path>
driver = webdriver.Chrome(chrome_driver_path)
Run Code Online (Sandbox Code Playgroud)
Sha*_*awn 42
如果您使用的 Selenium 版本是 v4.6.0 或更高版本(我认为正如我SeleniumManger在错误跟踪中看到的那样),那么您实际上不必设置路径driver.exe。Selenium 可以自行处理浏览器和驱动程序。
所以你的代码可以简化如下:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.google.com/")
driver.quit()
Run Code Online (Sandbox Code Playgroud)
一些参考:
这是由于 Selenium 4.10.0 中的更改所致: https://github.com/SeleniumHQ/selenium/commit/9f5801c82fb3be3d5850707c46c3f8176e3ccd8e

请注意,第一个参数不再是executable_path,并且desired_capabilities已被删除,但现在有另一种传递它的方法。请参阅升级到 Selenium 4以获取有关如何在使用 Selenium 4.10.0(或更高版本)时传递所需功能的文档)。
另外,如果您想设置executable_path,可以通过 传入service,但不再需要,因为包含了 selenium 管理器。
这是包含您需要的所有内容的代码片段:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
service = Service()
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)
# ...
driver.quit()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
86556 次 |
| 最近记录: |