TypeError: WebDriver.__init__() got an unexpected keyword argument 'executable_path' in Selenium Python
Asked Answered
G

7

49

My code:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

option = webdriver.ChromeOptions()
driver = webdriver.Chrome(executable_path='./chromedriver.exe', options=option)

driver.get('https://www.google.com/')

Output:

WebDriver.__init__() got an unexpected keyword argument 'executable_path'

I'm trying to create a script to log in to a website. When I try to run this script, it gives me this error: WebDriver.__init__() got an unexpected keyword argument 'executable_path'

Gramineous answered 25/6, 2023 at 12:55 Comment(0)
F
74

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 executable_path was removed.

If you want to pass in an executable_path, you'll have to use the service arg now.

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

service = Service(executable_path='./chromedriver.exe')
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)
# ...
driver.quit()
Frater answered 25/6, 2023 at 13:48 Comment(0)
H
24

Note: Remove executable_url from the argument, because you have installed the latest version of Selenium if you have Selenium above the 4.6.0, you don't need to add executable_url, and in the latest version of Selenium, you don't need to download webdriver.

Just copy the below code and run the your Python file simple.

from selenium import webdriver

driver=webdriver.Chrome()

driver.get("https://www.facebook.com/")
Haruspex answered 28/6, 2023 at 10:25 Comment(0)
S
6

Just remove executable_path(see below), if you do not wish to set the driver.exe path manually. With the latest Selenium (v4.6.0 and onwards), its in-built tool, known as SeleniumManger, can download and handle the driver.exe if you do not specify.

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

option = webdriver.ChromeOptions()
driver = webdriver.Chrome(options = option)

driver.get('https://www.google.com/')
Swedish answered 26/6, 2023 at 13:46 Comment(1)
In addition after this fix, while running selenium inside Docker container, I got error message: selenium.common.exceptions.WebDriverException: Message: Service /root/.cache/selenium/chromedriver/linux64/126.0.6478.126/chromedriver unexpectedly exited. Status code was: 127 When you run the command: /root/.cache/selenium/chromedriver/linux64/126.0.6478.126/chromedriver , you get the next error message: error while loading shared libraries: libnss3.so: cannot open shared object file: No such file or directory. Fix this by running: apt install libnss3Playboy
S
2

I helped solve this in this GitHub post: self.driver = driver_klass(**driver_kwargs) TypeError: WebDriver.init() got an unexpected keyword argument 'executable_path' #128. Please note I'm using Scrapy to create web scrapers and Selenium to interact with the website.

  • go to ton77v's commit 5c3fe7b and copy his code in middlewares.py
  • replace the middlewares.py code under the scrapy_selenium package on your local machine (for me, it was in C:/Users//AppData/Local/anaconda3/Lib/site-packages/scrapy_selenium/middlewares.py)
  • [optional]: I had to !pip install webdriver-manager. As well for your Scrapy spider, you need to modify the settings.py file (this is part of the configuration files that appear when you start a Scrapy project, like items.py, middlewares.py, pipelines.py, and settings.py). Add the following lines of code into the settings.py file
    • SELENIUM_DRIVER_NAME = 'chrome'
    • SELENIUM_DRIVER_EXECUTABLE_PATH = None #not actually necessary, will work even if you comment this line out
    • SELENIUM_DRIVER_ARGUMENTS=[] #put '--headless' in the brackets to prevent browser popup
  • then enter scrapy runspider <scraper_name>.py in your terminal and enjoy!

Quick explanation of what's happening:

  • you're getting Scrapy to install the BrowserDriverManager and don't have to specify the BrowserDriverManager location anymore
  • the beauty is that after the first BrowserDriverManager installation, it remembers the installation location and uses the installed BrowserDriverManager for subsequent runs
  • You can adapt the scraper to open other browsers by modifying middlewares.py file (get ChatGPT to do it for you XD) and changing SELENIUM_DRIVER_NAME = (browser name)
Sparerib answered 3/7, 2023 at 23:58 Comment(0)
S
2

You can try this method

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager

options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()), options=options)
Sarisarid answered 11/9, 2023 at 8:30 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Gracioso
P
1

The latest Selenium doesn’t require a webdriver. You can remove executable_url from the argument, because you have installed the latest version of Selenium.

Pretzel answered 1/8, 2023 at 14:53 Comment(0)
B
0

Try this:

from selenium.webdriver.firefox.service import Service as FirefoxService

firefox_profile = webdriver.FirefoxProfile()

firefox_profile.set_preference("browser.download.folderList", 2)
firefox_profile.set_preference("browser.download.manager.showWhenStarting", False)
# firefox_profile.set_preference("browser.download.dir", str(output_directory))
firefox_profile.set_preference(
    "browser.helperApps.neverAsk.saveToDisk", "application/zip")

service = FirefoxService(firefox_profile = firefox_profile)

browser = webdriver.Firefox(
    service= service,
    options = firefox_options
)
Biotin answered 25/11, 2023 at 4:3 Comment(3)
It is not even syntactically correct: IndentationError: unexpected indentFerreby
Is this a completely bogus answer?Ferreby
@PeterMortensen maybe i copy and paste so it has indent error just fix thatBiotin

© 2022 - 2024 — McMap. All rights reserved.