0

I just started using selenium with Python and I keep getting the following error code:

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

Here's the code for the context:

from selenium.webdriver import Firefox
from selenium.webdriver.common.keys import Keys

url = 'https://example'
driver_path = r"D:\path\to\geckodriver.exe"

browser = Firefox(executable_path=driver_path)
browser.get(url)

Thanks in advance!

I checked the path, the version of the selenium package and made sure that I have the right geckodriver.exe but still get the error.

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

4 Answers4

0

unexpected keyword argument means exactly that, you're trying to pass a keyword argument or named argument to a function that does not have that argument defined.

Remember there are 2 types of arguments in python, based on how you supply them to a function, keyword/named and positional.

def greet(name, age):
    return f"Hello, {name}! You are {age} years old."


# Using positional arguments, where order matters
result = greet("Leo", 30)
print(result)  # Output: Hello, Leo! You are 30 years old.
result = greet(30, "Leo")
print(result)  # Output: Hello, 30! You are Leo years old.
# Using keyword/named arguments, where order does not matter (unless you're mixing positional and keyword, then positionals go first, always)
result = greet(age=30, name="Leo")
print(result)  # Output: Hello, Leo! You are 30 years old.

Check the documentation for your version of selenium and check if, for the Firefox class, the init has an argument called executable_path.

Daviid
  • 630
  • 4
  • 17
0

You can simply use that :

from selenium.webdriver import Firefox
from selenium.webdriver.common.keys import Keys
url = 'https://example'
browser = Firefox()
browser.get(url)
Wahib Mzali
  • 120
  • 5
0

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

executable_path


Details

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

So when you mention:

browser = Firefox(executable_path=driver_path)

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 keyword Service(). So you need to change as follows:

from selenium.webdriver import Firefox

s = Service(r'D:\path\to\geckodriver.exe')
browser = Firefox(service=s)

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:

from selenium.webdriver import Firefox

browser = Firefox()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

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.firefox.service import Service

service = Service(executable_path="PATH_TO_GECKODRIVER")
options = webdriver.FirefoxOptions()
driver = webdriver.Firefox(service=service, options=options)
# ...
driver.quit()

But you no longer need to specify an executable_path due to a fully operational Selenium Manager in 4.10.0, so this is all you need:

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

service = Service()
options = webdriver.FirefoxOptions()
driver = webdriver.Firefox(service=service, options=options)
# ...
driver.quit()
Michael Mintz
  • 9,007
  • 6
  • 31
  • 48