Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DeprecationWarning: executable_path has been deprecated selenium python

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?

like image 957
YasserKhalil Avatar asked Nov 06 '20 15:11

YasserKhalil


People also ask

What is deprecated in Selenium?

internal package, is deprecated in Selenium 4. The changes are internal to the Selenium framework. Therefore, Selenium users can continue using the FindElement(By) and FindElements(By) as used in Selenium 3.

How do I update Selenium in Python?

C:\python36>pip install -U selenium The <pip> tool will download the latest version of the Selenium package and install it on your machine. The optional –U flag will upgrade the existing version of the installed package to the latest version. Hope this Helps!!

Is executable_path deprecated in selenium?

DeprecationWarning: executable_path has been deprecated, please pass in a Service object ...implies that the keyexecutable_pathwill be deprecated in the upcoming releases. This change is inline with the Selenium 4.0 Beta 1changelogwhich mentions:

What is deprecationwarning in Selenium 4?

What is DeprecationWarning: executable_path has been deprecated, please pass in a Service object? Let us take a simple example to demonstrate the issue. 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.

What does “ executable_path has been deprecated” mean?

DeprecationWarning: executable_path has been deprecated, please pass in a Service object means that the key executable_path will be deprecated in the upcoming releases. Once the key executable_path is deprecated you have to use an instance of the Service () class as follows:

What does the deprecationwarning mean?

DeprecationWarning: executable_path has been deprecated, please pass in a Service object ...implies that the keyexecutable_pathwill be deprecated in the upcoming releases.


2 Answers

I could figure it out

# 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  s=Service(ChromeDriverManager().install()) driver = webdriver.Chrome(service=s) driver.maximize_window() driver.get('https://www.google.com') driver.find_element(By.NAME, 'q').send_keys('Yasser Khalil') 
like image 147
YasserKhalil Avatar answered Oct 02 '22 14:10

YasserKhalil


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
like image 21
undetected Selenium Avatar answered Oct 02 '22 13:10

undetected Selenium