The executable_path is deprecated in Selenium 4 and above if you are still using the executable_path=chrome_driver_path) then you will get DeprecationWarning: executable_path has been deprecated, please pass in a Service object
In this tutorial, we will look at what exactly DeprecationWarning: executable_path has been deprecated, please pass in a Service object means and how to fix this issue with examples.
What is DeprecationWarning: executable_path has been deprecated, please pass in a Service object?
Let us take a simple example to demonstrate the issue.
from selenium import webdriver
chrome_driver_path = 'C:/Users/itsmycode/Documents/Dev/chromedriver.exe'
driver = webdriver.Chrome(executable_path=chrome_driver_path)
url = "https://www.google.com"
driver.get(url)
Output
main.py:3: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
driver = webdriver.Chrome(ChromeDriverManager().install())
In Selenium 4, the key executable_path is deprecated, and instead, we need to use an instance of the Service() class along with ChromeDriverManager().install() command.
This change is inline with the Selenium 4.0 Beta 1 changelog, which mentions:
Deprecate all but Options and Service arguments in driver instantiation. (#9125,#9128)
How to fix DeprecationWarning: executable_path has been deprecated, please pass in a Service object?
As the key executable_path is deprecated, we need to use an instance of the Service() class along with ChromeDriverManager().install().
Solution 1 – Using instance of service() class
Pre-requisites
Ensure that Selenium is upgraded to v4.0.0 and Webdriver Manager for Python is installed. If it is not done, just run the below commands.
pip3 install -U selenium
pip3 install webdriver-manager
Note: If you have not installed the webdriver-manager package properly, you will get ModuleNotFoundError: No module named 'webdriver_manager'
Selenium v4 compatible Code Block
Step 1: Import ChromeDriverManager from webdriver_manager.chrome
Step 2: As per Webdriver Manager for Python download_and_install() method isn’t supported, so you need to use install() method as shown in the below code snippet
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://www.google.com")
In case you want to pass the Options() object, you can use:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
options = Options()
options.add_argument("start-maximized")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
driver.get("https://www.google.com")
You can find the relevant Bug Report/Pull Request in:
- Bug Report: deprecate all but Options and Service arguments in driver instantiation
- Pull Request: deprecate all but Options and Service arguments in driver instantiation
Solution 2 – Another way of using service() instance
Instead of passing the chrome driver path to executable_path, we can pass the same to the Service() class as shown below.
Before
from selenium import webdriver
chrome_driver_path = 'C:/Users/itsmycode/Documents/Dev/chromedriver.exe'
driver = webdriver.Chrome(executable_path=chrome_driver_path)
url = "https://www.google.com"
driver.get(url)
After
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
s=Service('C:/Users/itsmycode/Documents/Dev/chromedriver.exe')
browser = webdriver.Chrome(service=s)
url='https://www.google.com'
browser.get(url)
Note: Ensure that chromedriver.exe path is correct else you will get Message: 'chromedriver.exe' executable needs to be in PATH error
Conclusion
The executable_path is deprecated in Selenium 4 and above, and using this will lead to DeprecationWarning: executable_path has been deprecated, please pass in a Service object warning message.
We can resolve this issue by using an instance of the Service() class along with ChromeDriverManager().install(). We also need to ensure that Selenium is upgraded to v4.0.0 and Webdriver Manager for Python is installed