Python Selenium: 'unexpected keyword argument 'executable_path'
Asked Answered
I

5

5

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.

Isidore answered 31/7, 2023 at 10:9 Comment(1)
What version of selenium do you have?Sadye
H
6

As mentioned in my earlier solution, https://mcmap.net/q/350307/-typeerror-webdriver-__init__-got-an-unexpected-keyword-argument-39-executable_path-39-in-selenium-python, 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()
Hereof answered 31/7, 2023 at 12:48 Comment(0)
S
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)
Sikang answered 31/7, 2023 at 11:19 Comment(0)
S
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()
Scathe answered 31/7, 2023 at 11:58 Comment(0)
B
0

those who are using Robot Framework(python) use can use code as below

Create Webdriver   Chrome Service(your driver location)

works for me!

Bottom answered 19/12, 2023 at 8:17 Comment(0)
W
-1

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.

Winniewinnifred answered 31/7, 2023 at 10:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.