selenium.common.exceptions.NoSuchDriverException:消息:无法使用 Selenium Manager 获取 chromedriver 错误使用 Selenium 和 ChromeDriver

selenium.common.exceptions.NoSuchDriverException: Message: Unable to obtain chromedriver using Selenium Manager error using Selenium and ChromeDriver

提问人:Polaretti 提问时间:7/15/2023 最后编辑:Super Kai - Kazuya ItoPolaretti 更新时间:9/22/2023 访问量:11000

问:

我不知道为什么我的代码总是出错

这是我的代码:

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)

Chromeder的路径:

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
python selenium-webdriver selenium-chromedriver seleniummanager

评论


答:

6赞 undetected Selenium 7/15/2023 #1

此错误消息...

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 或更高版本。


Selenium 管理器

在这种情况下,Selenium Manager 可以静默下载匹配的 ChromeDriver,您不必再明确提及 chromedriver 路径。


溶液

您的最小代码块可以是:

from selenium import webdriver

url = "https://google.com/"
driver = webdriver.Chrome()
driver.get(url)
1赞 Super Kai - Kazuya Ito 8/16/2023 #2

您应该删除路径以解决错误,如下所示。*这是推荐的,您可以看到我关于哪个版本的 chrome 驱动程序的问题的答案:webdriver.Chrome()webdriver.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赞 陳憲堂 9/22/2023 #3

编号 : https://pypi.org/project/webdriver-manager/#use-with-chrome

安装管理器:

pip 安装 webdriver-manager

与 Chrome for selenium 4 配合使用

   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()))

评论

0赞 Community 9/25/2023
您的答案可以通过其他支持信息进行改进。请编辑以添加更多详细信息,例如引文或文档,以便其他人可以确认您的答案是正确的。您可以在帮助中心找到有关如何写出好答案的更多信息。