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

4.6k Views Asked by At

I am having this problem in Whatsapp bot sending auto message in python.

Code trial:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import time

message = "this is message" 
amount = 50 
delay = 0.1 
contact = "person to message"

options = webdriver.ChromeOptions() 
options.add_argument('user-data-dir=C:\\Chrome_Profile') 

driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe", options=options)
driver.get('https://web.whatsapp.com/') 
#driver.minimize_window() 

time.sleep(20)

name_list = []

#//*[@id="pane-side"]/div[1]/div/div/div[11]/div/div/div/div[2]
#//*[@id="pane-side"]/div[1]/div/div/div[11]/div/div/div/div[2]/div[1]/div[1]/span

for i in range(1, 15):
    info = driver.find_element_by_xpath(f'//*[@id="pane-side"]/div[1]/div/div/div[{i}]' + '/div/div/div/div[2]/div[1]/div[1]/span')
    name_list.append(info.text)


to_click = name_list.index(contact) + 1


driver.find_element_by_xpath(f'//*[@id="pane-side"]/div[1]/div/div/div[{to_click}]').click()


time.sleep(2)

#//*[@id="main"]/footer/div[1]/div/span[2]/div/div[2]/div[1]/div/div[1]

for i in range(amount):
    # Searching for text box
    text_box = driver.find_element_by_xpath('//*[@id="main"]/footer/div[1]/div/span[2]/div/div[2]/div[1]/div/div[1]')
    text_box.send_keys(message)
    text_box.send_keys(Keys.ENTER) 
    time.sleep(delay) 

Error:

Traceback (most recent call last):
  File "c:\Users\Monster\OneDrive\Masaüstü\UserName\SPAMBOT PYTHON\spambot.py", line 14, in <module>
    driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe", options=options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: WebDriver.__init__() got an unexpected keyword argument 'executable_path'

So far, I tried Service library instead of driver keyword but no solution.Also the same code was working until about a few months ago with no change. But when I opened it today nothing changed my code didn't work

2

There are 2 best solutions below

0
Michael Mintz On BEST ANSWER

As mentioned in my earlier solution, https://stackoverflow.com/a/76550727/7058266, 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='C:\\chromedriver.exe')
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)
# ...
driver.quit()
0
undetected Selenium On

From the Commits in selenium-4.10.0:

executable_path

executable_path which was deprecated earlier is now completely removed from selenium-4.10.0 through the merge Remove deprecated code in driver classes.


Details

If you look at the constructor after self, the next argument is options followed by service.

So when you mention:

driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe", options=options)

Python interpreter doesn't recognizes the keyword executable_path anymore and raises

TypeError: WebDriver.init() got an unexpected keyword argument 'executable_path'

Solution

Using you have to use the key Service() . So you need to change as follows:

s = Service(r'C:\\chromedriver.exe')
driver = webdriver.Chrome(service=s, options=options)

Incase you are using Selenium v4.6 or above you don't have to explicitly use the Service() either as Selenium Manager can silently download the matching ChromeDriver as follows:

driver = webdriver.Chrome(options=options)