Unable to use Selenium Webdriver. Getting two exceptions
Asked Answered
Q

5

16

I am getting the following error when trying to create an 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)
Quickwitted answered 13/6, 2023 at 3:52 Comment(0)
S
52

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()
driver.get("https://www.google.com/")
driver.quit()

A few references:

Sardinian answered 13/6, 2023 at 8:35 Comment(4)
Simple and efective! Just updated the selenium version and don't need to have webdriver anymoreLustreware
Awesome Solution. hopefully the new selenium is less unnecessary commands. Great !Bischoff
I'm running selenium 4.15.2 and still get the NoSuchDriverException error when using this methodWheeling
Some guys tell install 4.6 version while Pypi has 4.16 as the latest version of selenium.Snapp
S
9

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 Upgrade to Selenium 4 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()
Sima answered 13/6, 2023 at 15:39 Comment(1)
I'm running 4.15.2 and still get the NoSuchDriverException error when using this methodWheeling
B
1

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_1.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_1.py
 └-chromedriver.exe # Here
Bosworth answered 16/8, 2023 at 14:41 Comment(0)
B
0

The version of Selenium which you are using is v4.6 or above. I run a correct code run my laptop, But when I run same code on another laptop, that have the error. I realized that it was installed selenium v4.16 . I install 3.141.0 version selenium and that work perfectly.

pip install selenium==3.141.0

Installing collected packages: selenium
  Attempting uninstall: selenium
    Found existing installation: selenium 4.16.0
    Uninstalling selenium-4.16.0:
      Successfully uninstalled selenium-4.16.0
Successfully installed selenium-3.141.0
Beaty answered 14/1 at 16:59 Comment(0)
A
0

I used time.sleep

Full code snippet:

from selenium import webdriver
import time

website = "website"
path = r'path'
driver = webdriver.Chrome( )
driver.get(website)
time.sleep(100)
driver.quit()

This worked for me, and you can add as much time as you need

Aestivation answered 21/3 at 17:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.