selenium.common.exceptions.NoSuchDriverException: Message: 使用Selenium Manager和ChromeDriver时无法获取chromedriver的错误

4
我搞不清楚为什么我的代码总是出错。
这是我的代码:
from selenium import webdriver

url = "https://google.com/"
path = "C:/Users/thefo/OneDrive/Desktop/summer 2023/chromedriver_win32"

driver = webdriver.Chrome(path)
driver.get(url)

Chromedriver的路径:

path from chromedriver

这是一直出现的错误:
Traceback (most recent call last):
  File "C:\Users\thefo\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\common\driver_finder.py", line 42, in get_path
    path = SeleniumManager().driver_location(options) if path is None else path
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\thefo\AppData\Local\Programs\Python\Python311\Lib\site-packages\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):
  File "c:\Users\thefo\OneDrive\Desktop\summer 2023\Projeto Bot Discord - BUFF SELL CHECKER\teste2.py", line 6, in <module>
    driver = webdriver.Chrome(path)
             ^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\thefo\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 47, in __init__
    self.service.path = DriverFinder.get_path(self.service, self.options)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\thefo\AppData\Local\Programs\Python\Python311\Lib\site-packages\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
3个回答

6
这个错误信息...
Traceback (most recent call last):
  File "C:\Users\thefo\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\common\driver_finder.py", line 42, in get_path
    path = SeleniumManager().driver_location(options) if path is None else path
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

这意味着您正在使用的Selenium版本是v4.6或更高版本。

自动化测试工具

在这种情况下,自动化测试工具可以静默下载匹配的ChromeDriver,您不再需要显式地提及chromedriver路径。


解决方案

你的最简代码块可以是:

from selenium import webdriver

url = "https://google.com/"
driver = webdriver.Chrome()
driver.get(url)

1
你应该从webdriver.Chrome()中删除路径以解决下面显示的错误。*这是推荐的做法,你可以查看我的问题的答案,了解webdriver.Chrome()使用的Chrome驱动程序版本。
from selenium import webdriver

url = "https://google.com/"
# path = "C:/Users/thefo/OneDrive/Desktop/summer 2023/chromedriver_win32"

driver = webdriver.Chrome() # Here
driver.get(url)

或者,你应该将路径设置为Service()并将其设置为webdriver.Chrome()以解决如下所示的错误:
from selenium.webdriver.chrome.service import Service
from selenium import webdriver

url = "https://google.com/"
path = "C:/Users/thefo/OneDrive/Desktop/summer 2023/chromedriver_win32"

service = Service(executable_path=path) # Here
driver = webdriver.Chrome(service=service) # Here
driver.get(url)

0
ref: https://pypi.org/project/webdriver-manager/#use-with-chrome

安装管理器:

使用以下命令安装webdriver-manager:

pip install webdriver-manager

在selenium 4中使用Chrome:

   from selenium import webdriver
   from selenium.webdriver.chrome.service import Service as ChromeService
   from webdriver_manager.chrome import ChromeDriverManager

   driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))

你的回答可以通过提供更多支持性信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的回答是否正确。你可以在帮助中心找到关于如何撰写好回答的更多信息。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接