Huu Quy
Huu Quy

Reputation: 541

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

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'

Upvotes: 54

Views: 194050

Answers (7)

LongDev
LongDev

Reputation: 221

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
)

Upvotes: 0

Michael Mintz
Michael Mintz

Reputation: 15556

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()

Upvotes: 84

xhlijinlong
xhlijinlong

Reputation: 21

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)

Upvotes: 2

Medi Monam
Medi Monam

Reputation: 11

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.

Upvotes: 1

Jerry
Jerry

Reputation: 41

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)

Upvotes: 2

Arsalan Ahmed
Arsalan Ahmed

Reputation: 271

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/")

Upvotes: 27

Shawn
Shawn

Reputation: 8508

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/')

Upvotes: 6

Related Questions