[Fixed] DeprecationWarning: executable_path has been deprecated, please pass in a Service object

In Selenium 4 and later, the executable path is deprecated; if you continue to use it (executable path=chrome driver path), you will receive a Deprecation error. Warning: The executable path function has been deprecated; instead, use a Service object.

With the help of examples, we will look at what DeprecationWarning: executable path has been deprecated, please pass in a Service object means and how to resolve the issue it represents.

DeprecationWarning: executable_path has been deprecated, please pass in a Service object Meaning?

To understand why this error is being displayed let us see an example that would cause this error to occur.

//Importing selenium 
from selenium import webdriver

YourChromeDriverPath = 'C:/Users/Coduber/Documents/Dev/chromedriver.exe'
newWebDriver = webdriver.Chrome(executable_path=YourChromeDriverPath)
 
url = "https://stackoverflow.com"
newWebDriver.get(url)

Output:

test.py:3: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
driver = webdriver.Chrome(ChromeDriverManager().install())

Instead of using an instance of the Service() class in conjunction with the ChromeDriverManager().install() command, we must use an instance of the Service() class in Selenium 4.

That is consistent with the Selenium 4.0 Beta 1 changelog, which states: Deprecate all arguments in driver instantiation except the Options and Service arguments. (#9125,#9128).

Fix DeprecationWarning: executable_path has been deprecated, please pass in a Service object?

Due to the deprecation of the executable path key, we must use an instance of the Service() class in conjunction with the ChromeDriverManager (). install().

Method 1: Using an instance of service() class

In order to complete this task, ensure that Selenium is upgraded to version 4.0.0 and that Webdriver Manager for Python is installed. If it hasn’t been completed yet, simply run the commands listed below.

pip install -U selenium
pip install -U webdriver

Note: If you have not properly installed the webdriver-manager package, you will receive the ModuleNotFoundError: No module named ‘webdriver_manager’ error message.

  • ChromeDriverManager should be imported from webdriver_manager.chrome.
  • Make a list of all of the things you want to do. According to Webdriver Manager for Python, the download and install() method is not supported; therefore, you must use the install() method, as shown in the following code snippet.
#Importing Required Library
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
 
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get("https://stackoverflow.com")

If you need to pass something else through an optional function, use the code below.

#Importing Required Library
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

#Sending an Optional argument
options = Options()
options.add_argument("start-maximized")
 
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get("https://stackoverflow.com")
[Fixed] DeprecationWarning: executable_path has been deprecated, please pass in a Service object

A bug report/pull request can be found here:

Method 2: Using Service() Function Differently

Rather than passing executable path the path to the chrome driver, we can instead pass it to the Service() class, as shown in the code below.

#importing required Library
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
 
s=Service('C:/Users/coduber/Documents/Dev/chromedriver.exe')
browser = webdriver.Chrome(service=s)
url='https://stackoverflow.com'
browser.get(url)

Note: If the path to chromedriver.exe is incorrect, you will receive this error message. Message: Error: ‘chromedriver.exe’ must be in the PATH.

[Fixed] DeprecationWarning: executable_path has been deprecated, please pass in a Service object

Wrap Up

The executable path variable is deprecated in Selenium 4 and higher, and using it will result in a DeprecationException being raised. Warning: The executable path parameter has been deprecated; please pass in a Service object warning message instead.

It is possible to resolve this issue by utilizing an instance of the Service() class in conjunction with the ChromeDriverManager().install method (). We also need to make sure that Selenium is updated to version 4.0.0 and that Webdriver Manager for Python is properly installed and configured.

Further Read:

  1. Python Random Module – Generate Random Numbers/Sequences
  2. Pandas Update Row Value In Python DataFrame
  3. [Fixed] Python TypeError: unhashable type: ‘list’
  4. Python How To Add A New Line To A String

Leave a Comment