init() got an unexpected keyword argument ‘chrome_options’ TypeError in Selenium

init() got an unexpected keyword argument ‘chrome_options’ TypeError in Selenium. When working with Selenium, you might encounter the error TypeError: __init__() got an unexpected keyword argument 'chrome_options'. This error typically occurs when there is a mismatch between the version of Selenium you are using and the way you are initializing the WebDriver. This article will explain the cause of this error and provide solutions to resolve it.

Cause of the Error

The chrome_options keyword argument was used in older versions of Selenium to pass options to the Chrome WebDriver. However, in Selenium 4, the chrome_options argument has been deprecated and replaced with options. If you are using Selenium 4 and still using chrome_options, you will encounter this error.

Solution

To resolve this error, you need to update your code to use the options argument instead of chrome_options. Below are examples of how to correctly initialize the Chrome WebDriver in both Selenium 3 and Selenium 4.

Selenium 3

In Selenium 3, you can use the chrome_options argument as follows:

Python
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

# Create Chrome options
chrome_options = Options()

# Initialize the WebDriver with chrome_options
driver = webdriver.Chrome(executable_path='C:\Program Files\Chrome Driver\chromedriver.exe', chrome_options=chrome_options)

Selenium 4

In Selenium 4, you should use the options argument instead of chrome_options:

Python
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options

# Create Chrome options
options = Options()

# Initialize the Service object
service = Service('C:\Program Files\Chrome Driver\chromedriver.exe')

# Initialize the WebDriver with options
driver = webdriver.Chrome(service=service, options=options)

Conclusion

The TypeError: __init__() got an unexpected keyword argument 'chrome_options' error is a common issue when transitioning from Selenium 3 to Selenium 4. By updating your code to use the options argument instead of chrome_options, you can resolve this error and ensure compatibility with the latest version of Selenium.

Leave a Comment

Your email address will not be published. Required fields are marked *