0

I'm trying to create a selenium code in my computer but it is not working. The code is:

    from selenium import webdriver
    from pathlib import Path

    chromedriver_path:str = Path(__file__).absolute().parent / 'chromedriver'
    driver = webdriver.Chrome(chromedriver_path)
    driver.get("www.google.com")

When I try to run it, it shows me the following error

Traceback (most recent call last):
  File "/Users/macklee/anaconda3/lib/python3.11/site-packages/selenium/webdriver/common/driver_finder.py", line 38, in get_path
    path = SeleniumManager().driver_location(options) if path is None else path
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/macklee/anaconda3/lib/python3.11/site-packages/selenium/webdriver/common/selenium_manager.py", line 87, in driver_location
    browser = options.capabilities["browserName"]
              ^^^^^^^^^^^^^^^^^^^^
AttributeError: 'PosixPath' object has no attribute 'capabilities'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/macklee/Desktop/Computer science/Studies/Python/Paulla gomes project/navagation.py", line 5, in 
    driver = webdriver.Chrome(chromedriver_path)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/macklee/anaconda3/lib/python3.11/site-packages/selenium/webdriver/chrome/webdriver.py", line 45, in __init__
    super().__init__(
  File "/Users/macklee/anaconda3/lib/python3.11/site-packages/selenium/webdriver/chromium/webdriver.py", line 49, in __init__
    self.service.path = DriverFinder.get_path(self.service, options)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/macklee/anaconda3/lib/python3.11/site-packages/selenium/webdriver/common/driver_finder.py", line 40, in get_path
    msg = f"Unable to obtain driver for {options.capabilities['browserName']} using Selenium Manager."
                                         ^^^^^^^^^^^^^^^^^^^^
AttributeError: 'PosixPath' object has no attribute 'capabilities'
2
  • Try this path = Path(__file__).absolute().parent / "chromedriver.exe" or try reinstalling the selenium package. Commented Jan 26, 2024 at 16:27
  • @sourin_karmakar Aside from the presence of ".exe" on the end, how is your suggestion any different from what they used? Commented Jan 26, 2024 at 16:35

2 Answers 2

0

By default, Chrome() assumes that its first argument is an options object.

You need to specify that you're passing the chromedriver path, not an options object.

You do that by putting executable_path= in fromnt of the argument.

driver = webdriver.Chrome(executable_path=chromedriver_path)
Sign up to request clarification or add additional context in comments.

1 Comment

It gave me another error: Traceback (most recent call last): File "/Users/macklee/Desktop/Computer science/Studies/Python/Paulla gomes project/navagation.py", line 5, in <module> driver = webdriver.Chrome(executable_path=chromedriver_path) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: WebDriver.__init__() got an unexpected keyword argument 'executable_path'
0

You can try this out

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

service = Service()
driver = webdriver.Chrome(service=service)
driver.get("www.google.com")

Service will autodetect the chrome version and download suitable chromedriver. This will happen in the background. You might need selenium>=4.10

1 Comment

Creating a Service with default/no args really does nothing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.