Answer a question

I am using sublime to code python scripts. The following code is for selenium in python to install the driver automatically by using the webdriver_manager package

# pip install webdriver-manager
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()

#s=Service(path)
#driver=webdriver.Chrome(service=s)
driver.get('https://www.google.com')
driver.find_element(By.NAME, 'q').send_keys('Yasser Khalil')

The code works fine but I got a warning like that

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

How to fix such a bug?

Answers

This error message...

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

...implies that the key executable_path will be deprecated in the upcoming releases.

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)


Solution

With selenium4 as the key executable_path is deprecated you have to use an instance of the Service() class along with ChromeDriverManager().install() command as discussed below.

Pre-requisites

Ensure that:

  • Selenium is upgraded to v4.0.0

    pip3 install -U selenium
    
  • Webdriver Manager for Python is installed

    pip3 install webdriver-manager
    

You can find a detailed discussion on installing Webdriver Manager for Python in ModuleNotFoundError: No module named 'webdriver_manager' error even after installing webdrivermanager

Selenium v4 compatible Code Block

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")

Console Output:

[WDM] - ====== WebDriver manager ======
[WDM] - Current google-chrome version is 96.0.4664
[WDM] - Get LATEST driver version for 96.0.4664
[WDM] - Driver [C:\Users\Admin\.wdm\drivers\chromedriver\win32\96.0.4664.45\chromedriver.exe] found in cache

You can find a detailed discussion on installing Webdriver Manager for Python in Selenium ChromeDriver issue using Webdriver Manager for Python


Incase 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")

TL; DR

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
Logo

学AI,认准AI Studio!GPU算力,限时免费领,邀请好友解锁更多惊喜福利 >>>

更多推荐