0

Here is my code I tried different path versions but I am not able to run this code.

from selenium import webdriver

# Specify the path to the Chrome WebDriver executable
PATH = "C:\Users\CHEEMN2\Downloads\chromedriver-win64\chromedriver-win64/to/chromedriver.exe"  # Replace with the actual path on your system

# Create a Chrome WebDriver instance
driver = webdriver.Chrome(executable_path=PATH)

# Open a URL
driver.get("https://www.google.com/")

# Close the WebDriver when done
driver.quit()

This is the error i keep on getting

[Running] python -u "c:\Users\CHEEMN2\OneDrive - CPGGPC\Python\import os.py"
Traceback (most recent call last):
  File "C:\Users\CHEEMN2\AppData\Local\Programs\Python\Python311\Lib\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 "C:\Users\CHEEMN2\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\common\selenium_manager.py", line 71, 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\CHEEMN2\OneDrive - CPGGPC\Python\import os.py", line 6, in <module>
    driver = webdriver.Chrome(driver_path)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\CHEEMN2\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 45, in __init__
    super().__init__(
  File "C:\Users\CHEEMN2\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 51, in __init__
    self.service.path = DriverFinder.get_path(self.service, options)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\CHEEMN2\AppData\Local\Programs\Python\Python311\Lib\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: 'str' object has no attribute 'capabilities'

[Done] exited with code=1 in 0.683 seconds

I am using VS code for web scraping.

I have tried to to check all the versions for Python 3.11.4

I downloaded this version for Chrome chrome version 116.0.5845.97 (Official Build) (64-bit) (cohort: Bypass) chromedriver win64 https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/116.0.5845.96/win64/chromedriver-win64.zip

the selenium version is python -c "import selenium; print(selenium.version)" 4.11.2

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

2 Answers2

1

Let the built-in driver manager of selenium 4.11.2 do all the work for you. You don't need to specify a path anymore (and you can't pass it directly into the Chrome() call ... use the Service() arg instead.)

Here's code that launches Chrome, while giving you the ability to change options via ChromeOptions() and Service():

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

service = Service()
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)
# ... Anything you want to automate ...
driver.quit()
Michael Mintz
  • 9,007
  • 6
  • 31
  • 48
0

Since you are using selenium v4.11.2, you do not have to manually provide the path of driver. Selenium can download and manage it for you.

Your code can be simplified to:

from selenium import webdriver

# Create a Chrome WebDriver instance
driver = webdriver.Chrome()

# Open a URL
driver.get("https://www.google.com/")

# Close the WebDriver when done
driver.quit()

For more info:

Shawn
  • 4,064
  • 2
  • 11
  • 23