Last updated: Apr 10, 2024
Reading timeยท2 min
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.
/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())
Here is an example of using the newer Service class.
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.
You have to make sure that you have webdriver-manager
and
selenium installed to run the code
snippet.
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.
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.
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.
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.
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.
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.
You can learn more about the related topics by checking out the following tutorials: