得到两个异常,无法使用 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
这是我使用的代码:
from selenium import webdriver
chrome_driver_path = <chrome drive .exe path>
driver = webdriver.Chrome(chrome_driver_path)
如果您使用的selenium版本是v4.6.0
或更高版本(我认为这是我在错误跟踪中看到的SeleniumManger
),那么您实际上不必设置driver.exe
路径。 Selenium 可以自行处理浏览器和驱动程序。
所以你的代码可以简化如下:
from selenium import webdriver
driver = webdriver.Chrome()
参考资料:
这是由于selenium
4.10.0
的变化造成的:
https://github.com/SeleniumHQ/selenium/commit/9f5801c82fb3be3d5850707c46c3f8 176e3ccd8e
请注意,第一个参数不再是 executable_path
,并且 desired_capabilities
已被删除,但现在有另一种传递它的方法。 有关如何使用selenium
4.10.0
(或更新版本)时传递所需功能的文档,请参阅https://www.selenium.dev/documentation/webdriver/getting_started/upgrade_to_selenium_4/#capability。
另外,如果你想设置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()
我得到了同样的错误如下:
AttributeError: 'str' object has no attribute 'capabilities'
因为我将chromedriver.exe
的路径设置为webdriver.Chrome()
,如下:
from selenium import webdriver
driver = webdriver.Chrome('./chromedriver.exe')
所以,我删除了webdriver.Chrome()
中的路径,如下所示,然后错误就解决了。 *这是推荐的,您可以查看 我的问题关于webdriver.Chrome()
获得哪个版本的chrome驱动程序:
from selenium import webdriver
driver = webdriver.Chrome()
或者,我将路径设置为 Service() 并将其设置为webdriver.Chrome()
,如下所示,则错误解决:
from selenium.webdriver.chrome.service import Service
from selenium import webdriver
service = Service(executable_path='./chromedriver.exe')
driver = webdriver.Chrome(service=service)
而且,我遇到了下面同样的错误,因为我没有在 django-project
中下载并设置 chromedriver.exe :
selenium.common.exceptions.NoSuchDriverException: Message: Unable to locate or obtain driver for chrome; For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors/driver_location
这是我的代码:
# "tests/test_1.py"
from django.test import LiveServerTestCase
from selenium.webdriver.chrome.service import Service
from selenium import webdriver
class TestBrowser1(LiveServerTestCase):
def test_example(self):
service = Service(executable_path='./chromedriver')
driver = webdriver.Chrome(service=service)
driver.get(("%s%s" % (self.live_server_url, "/admin/")))
assert "Log in | Django site admin" in driver.title
于是,我下载了chromedriver.exe
并将其设置到django-project
中的根目录,如下所示,然后错误就解决了:
django-project
|-core
| |-settings.py
| └-urls.py
|-my_app1
|-my_app2
|-tests
| |-__init__.py
| └-test_1.py
└-chromedriver.exe # Here