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

回答 2 浏览 1988 2023-07-14

我不明白为什么我的代码总是出错

这是我的代码:

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
Polaretti 提问于2023-07-14
2 个回答
#1楼 已采纳
得票数 4

这个错误信息...

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 Manager

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


解决方案

您的最小代码块可以是:

from selenium import webdriver

url = "https://google.com/"
driver = webdriver.Chrome()
driver.get(url)
undetected Selenium 提问于2023-07-14
undetected Selenium 修改于2023-08-03
#2楼
得票数 0

您应该删除 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)
Super Kai - Kazuya Ito 提问于2023-08-16
Super Kai - Kazuya Ito 修改于2023-08-16