14

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'

Michael Mintz
  • 9,007
  • 6
  • 31
  • 48
Huu Quy
  • 141
  • 1
  • 1
  • 3

5 Answers5

25

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()
Michael Mintz
  • 9,007
  • 6
  • 31
  • 48
9

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

Just remove executable_path(see below), if you do not wish to set the driver.exe path manually. With 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/')
Shawn
  • 4,064
  • 2
  • 11
  • 23
1

the latest selenium doesnt require a webdriver. you can remove executable_url from the argument, because you have installed the latest version of Selenium

Medi Monam
  • 11
  • 1
0

I helped solve this in this github post: https://github.com/clemfromspace/scrapy-selenium/issues/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)
Jerry
  • 11
  • 2