DeprecationWarning: executable_path has been deprecated

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
2 min

banner

# DeprecationWarning: executable_path has been deprecated

The Selenium warning "DeprecationWarning: executable_path has been deprecated please pass in a Service object" occurs because starting with Selenium version 4.0, the executable_path argument has been deprecated.

To resolve the issue, use an instance of the Service class instead.

shell
/home/borislav/Desktop/bobbyhadz_python/main.py:5: DeprecationWarning: executable_path has been deprecated, please pass in a Service object driver = webdriver.Chrome(ChromeDriverManager().install())

# Using the newer Service class in Selenium 4

Here is an example of using the newer Service class.

main.py
import time 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("http://www.python.org") time.sleep(2) driver.close()

We passed an instance of the Service class to the Chrome class.

# Make sure you have the modules installed

You have to make sure that you have webdriver-manager and selenium installed to run the code snippet.

shell
pip install webdriver-manager selenium --upgrade # ๐Ÿ‘‡๏ธ For Python 3 pip3 install webdriver-manager selenium --upgrade # ๐Ÿ‘‡๏ธ If you get a permissions error sudo pip3 install webdriver-manager selenium --upgrade # ๐Ÿ‘‡๏ธ If you don't have pip in your PATH environment variable python -m pip install webdriver-manager selenium --upgrade # ๐Ÿ‘‡๏ธ For python 3 python3 -m pip install webdriver-manager selenium --upgrade # ๐Ÿ‘‡๏ธ Using py alias py -m pip install webdriver-manager selenium --upgrade # ๐Ÿ‘‡๏ธ Alternative if you get a permissions error pip install webdriver-manager selenium --user --upgrade

You can pass the Options object as a keyword argument when instantiating the Chrome class.

main.py
import time from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options from webdriver_manager.chrome import ChromeDriverManager options = Options() options.add_argument("start-maximized") options.add_argument("disable-extensions") driver = webdriver.Chrome( service=Service(ChromeDriverManager().install()), options=options ) driver.get("http://www.python.org") time.sleep(2) driver.close()

We created an options object and passed it as a keyword argument to the Chrome class when instantiating it.

Here is an example that uses the Service class with Chromium.

main.py
import time from selenium import webdriver from selenium.webdriver.chrome.service import Service from webdriver_manager.chrome import ChromeDriverManager from webdriver_manager.core.utils import ChromeType driver = webdriver.Chrome(service=Service( ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install())) driver.get("http://www.python.org") time.sleep(2) driver.close()

And here is an example of using the Service class with Firefox.

main.py
import time from selenium import webdriver from selenium.webdriver.firefox.service import Service from webdriver_manager.firefox import GeckoDriverManager driver = webdriver.Firefox( service=Service(GeckoDriverManager().install())) driver.get("http://www.python.org") time.sleep(2) driver.close()

The code samples assume that you use Selenium version > 4.

You can check out other examples in the pypi page of webdriver-manager.

You can also pass a path to the Service class.

main.py
import time from selenium import webdriver from selenium.webdriver.chrome.service import Service driver = webdriver.Chrome(service=Service( r'C:/Users/bobbyhadz/Documents/chromedriver.exe')) driver.get("http://www.python.org") time.sleep(2) driver.close()

Make sure to specify the correct path to your executable.

Here is a complete example of starting a Chrome instance, visiting Google and typing into the search field.

main.py
import time from selenium import webdriver from selenium.webdriver.common.by import By 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") time.sleep(4) search_field = driver.find_element(By.NAME, 'q') search_field.send_keys('bobbyhadz.com') search_field.submit() time.sleep(2) driver.close()

If you need to check out some examples of how to locate elements on a page using Selenium, check out this section of the docs.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev