3

Getting the following error when trying to create object with 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

This is the code I used:

from selenium import webdriver

chrome_driver_path = <chrome drive .exe path>
driver = webdriver.Chrome(chrome_driver_path)
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
startrek-07
  • 33
  • 1
  • 1
  • 6

3 Answers3

17

If the selenium version you are using is v4.6.0 or above (which I think it is as I see SeleniumManger in the error trace), then you don't really have to set the driver.exe path. Selenium can handle the browser and drivers by itself.

So your code can be simplified as below:

from selenium import webdriver

driver = webdriver.Chrome()

Few references:

Shawn
  • 4,064
  • 2
  • 11
  • 23
1

This is due to changes in selenium 4.10.0: https://github.com/SeleniumHQ/selenium/commit/9f5801c82fb3be3d5850707c46c3f8176e3ccd8e

Changes_in_selenium_4_10_0

Note that the first argument is no longer executable_path, and that desired_capabilities has been removed, but there is now another way of passing it in. See https://www.selenium.dev/documentation/webdriver/getting_started/upgrade_to_selenium_4/#capabilities for the documentation on how to pass in desired capabilities when using selenium 4.10.0 (or newer).

Also, if you want to set an executable_path, it can be passed in via the service, but it is no longer necessary, as selenium manager is included.

Here's a code snippet with everything you need:

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()
Michael Mintz
  • 9,007
  • 6
  • 31
  • 48
0

I got the same error below:

AttributeError: 'str' object has no attribute 'capabilities'

Because I set the path of chromedriver.exe to webdriver.Chrome() as shown below:

from selenium import webdriver

driver = webdriver.Chrome('./chromedriver.exe')

So, I removed the path from webdriver.Chrome() as shown below, then the error was solved. *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

driver = webdriver.Chrome()

Or, I set the path to Service() and set it to webdriver.Chrome() as shown below, then the error was solved:

from selenium.webdriver.chrome.service import Service
from selenium import webdriver

service = Service(executable_path='./chromedriver.exe')
driver = webdriver.Chrome(service=service)

And, I got the same error below because I did not download and set chromedriver.exe in django-project:

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

This is my code:

# "tests/test_ex1.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

So, I downloaded chromedriver.exe and set it to the root directory in django-project as shown below, then the error was solved:

django-project
 |-core
 |  |-settings.py
 |  └-urls.py
 |-my_app1
 |-my_app2
 |-tests
 |  |-__init__.py
 |  └-test_ex1.py
 └-chromedriver.exe # Here
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129