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

Question:

I can’t figure out why my code it’s always getting an error

This is my code:

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)

Path of chromedriver:

path from chromedriver

and this is the error that always appears:

Traceback (most recent call last):
  File "C:UsersthefoAppDataLocalProgramsPythonPython311Libsite-packagesseleniumwebdrivercommondriver_finder.py", line 42, in get_path
    path = SeleniumManager().driver_location(options) if path is None else path
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:UsersthefoAppDataLocalProgramsPythonPython311Libsite-packagesseleniumwebdrivercommonselenium_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:UsersthefoOneDriveDesktopsummer 2023Projeto Bot Discord - BUFF SELL CHECKERteste2.py", line 6, in <module>
    driver = webdriver.Chrome(path)
             ^^^^^^^^^^^^^^^^^^^^^^
  File "C:UsersthefoAppDataLocalProgramsPythonPython311Libsite-packagesseleniumwebdriverchromewebdriver.py", line 47, in __init__
    self.service.path = DriverFinder.get_path(self.service, self.options)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:UsersthefoAppDataLocalProgramsPythonPython311Libsite-packagesseleniumwebdrivercommondriver_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
Asked By: Polaretti

||

Answers:

This error message…

Traceback (most recent call last):
  File "C:UsersthefoAppDataLocalProgramsPythonPython311Libsite-packagesseleniumwebdrivercommondriver_finder.py", line 42, in get_path
    path = SeleniumManager().driver_location(options) if path is None else path
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

…implies that the version of Selenium which you are using is v4.6 or above.


Selenium Manager

In such cases Selenium Manager can silently download the matching ChromeDriver and you don’t have to explicitly mention the chromedriver path anymore.


Solution

Your minimal code block can be:

from selenium import webdriver

url = "https://google.com/"
driver = webdriver.Chrome()
driver.get(url)
Answered By: undetected Selenium

You should remove the path from webdriver.Chrome() to solve the error as shown below. *This is recommended and you can see the answers of my question about which version of chrome driver webdriver.Chrome() gets:

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)

Or, you should set the path to Service() and set it to webdriver.Chrome() to solve the error as shown below:

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)