DeprecationWarning: executable_path has been deprecated selenium python
Asked Answered
S

11

201

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?

Stereochrome answered 6/11, 2020 at 15:22 Comment(0)
C
266

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 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:

Collen answered 24/11, 2021 at 15:52 Comment(16)
Thank you very much. I have tried it but still get TypeError: __init__() got an unexpected keyword argument 'service'. Any ideas?Stereochrome
Are you sure you did pip3 install -U seleniumCollen
Ah, I got you, you did pip install webdriver-manager, where as you need pip install webdriver_manager See ModuleNotFoundError: No module named 'webdriver_manager' error even after installing webdrivermanagerCollen
I have used pip install webdriver_manager and tried again but still the same error. it is so weird.Stereochrome
Delete the .wdm directory and redo the steps.Collen
Thanks a lot. I have used this line pip3 install -U selenium and it seems that this solved the problem. What does -U mean?Stereochrome
-U for` upgrade`Collen
chromeDriverManager returns response "not available in your country" I could go around it with a VPN but is there an alternative to it ?Incidentally
This is a side note but I don't get the point of adding TL;DR at the end of the post. Isn't it most helpful to have it first as a digest of a lengthy block of text that follows?Belt
I get frustrated with most deprecations but this one makes selenium a lot less tedious to set up so I'm happy.Potoroo
“Exception: Can not find browser google-chrome installed in your system!!!”Cradle
@AlexQuinn Having google-chrome installed in your system is mandatory.Collen
If anyone else finds their way here because pyhtml2pdf was affected by this breakage, there's a pull request based on this answer that fixes the problem: github.com/kumaF/pyhtml2pdf/pull/2Easterly
This approach has also its own issue. Currently Google version 115 has already been updated on my Chrome whether it is Google Chrome or Brave. By using the solution, it will take the latest release of chromedriver that follow the chrome we used, however LATEST version of 115 is not released yet. However if your Chrome is below 115. then it can be used. Only if you used the latest chrome.Cavie
I have selenium 4.8.3 on Debian -- if I pass the executable_path as an option to Service() there is no deprecation warning, so installing webdriver-manager doesn't appear to be strictly needed. Eg. this works (no warning generated): service=Service(executable_path=/usr/bin/chromedriver)Auditor
If "pip3 install webdriver_manager" (with an underscore) is correct, can you edit your top comment? you still have webdriver-hyphen-manager there.Fastback
M
105

This works for me

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
service = Service(r"C:\chromedriver.exe")
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)

Extending on the accepted answer, the Service class allows to explicitly specify a ChromeDriver executable in the same way as previously using the executable_path parameter. In this way existing code is easily migrated (clearly you need to replace C:\chromedriver.exe above by your path).

Magness answered 9/11, 2021 at 3:47 Comment(4)
This works for me. The accepted answer does not.Cradle
Love this answer! No need to install an additional package!Eyeshade
An even simpler answer chromedriver = "chromedriver.exe" s = Service(chromedriver) works for me.Cartier
The accepted answer is thorough but doesn't explain how to use service instead of using executable_path as we were before.Bouchier
S
71

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')
Stereochrome answered 6/11, 2020 at 17:46 Comment(3)
I am getting : driver = webdriver.Chrome(service=chrome_service, options=options) TypeError: __init__() got an unexpected keyword argument 'service' Does this still work for you?Eucalyptol
No, it doesn't work for me now (I don't know why and I have searched to find a solution but didn't find one)Stereochrome
It turns out I was mixing two separate virtual environments, one had version 3.x installed and the other one version 4.0. In version 4./0 it does work for me (but using executable_path in the Service, not ChromeDriverManager().install() )Eucalyptol
J
59

UPDATE Nov 2023

For Chrome Version 119.0.6045.124 (Official Build) (64-bit) or newer, there isn't any WebDriver as before. So just add these codes:

from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.selenium.dev/selenium/web/web-form.html")

I found this deprecation issue is appearing on Selenium, Pip and Python updates. So simply just change :

before:

from selenium import webdriver
chrome_driver_path = 'C:/Users/Morteza/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/Morteza/Documents/Dev/chromedriver.exe')
browser = webdriver.Chrome(service=s)
url='https://www.google.com'
browser.get(url)
Jungle answered 8/11, 2021 at 15:7 Comment(0)
A
19

All the above answers refer to Chrome, adding the one for Firefox

Install:

pip install webdriver-manager

Code:

from selenium.webdriver.firefox.service import Service
from webdriver_manager.firefox import GeckoDriverManager
driver = webdriver.Firefox(service=Service(executable_path=GeckoDriverManager().install()))

Reference: https://github.com/SergeyPirogov/webdriver_manager/issues/262#issuecomment-955197860

Airlift answered 26/3, 2022 at 14:13 Comment(1)
I didn't want to pip install anything, so I was able to skip the import GeckoDriverManager, and just import Service and run: driver = webdriver.Firefox(service=Service(executable_path="/usr/local/bin/geckodriver"))Horologe
O
8
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
service_obj = Service("WebDrivers_path\chromedriver.exe")
driver = webdriver.Chrome(service=service_obj)
driver.get("https://www.google.com")
Oligarchy answered 6/5, 2022 at 7:1 Comment(0)
C
5

Have a look at the new definition in the Service object here.

My solution

from selenium.webdriver.chrome.service import Service

chrome_executable = Service(executable_path='chromedriver.exe', log_path='NUL')
driver = webdriver.Chrome(service=chrome_executable)
Clubfoot answered 22/6, 2022 at 8:55 Comment(0)
C
4

Simplest option with Chrome auto-installer:

from selenium import webdriver    
import chromedriver_autoinstaller
from selenium.webdriver.chrome.service import Service

chromedriver_autoinstaller.install()
driver = webdriver.Chrome(service=Service())
Continence answered 19/12, 2021 at 6:10 Comment(0)
T
2

For those of you using Selenium v4.6.0 or above:

No need to set path of driver.exe anymore, nor you need an external library such as webdriber-manager. Selenium's in-built tool known as Selenium Manager will handle the browser drivers.

No need of below code:

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

# Or
s = Service('C:/Users/Downloads/chromedriver/chromedriver.exe')
driver = webdriver.Chrome(service=s)

# Or
driver = webdriver.Chrome('/path/to/chromedriver')

Code can be as simple as:

from selenium import webdriver

driver = webdriver.Chrome()
driver.get("https://www.google.com")

References:

Tradesfolk answered 28/6, 2023 at 14:39 Comment(0)
M
1

if you are using any IDE like PyCharm install webdriver-manager package of that IDE as how do install for selenium package

Mccubbin answered 27/9, 2021 at 10:27 Comment(0)
C
-1

You can create an instance of ChromeOptions, which has convenient methods for setting ChromeDriver-specific capabilities. You can then pass the ChromeOptions object into the ChromeDriver constructor:

ChromeOptions options = new ChromeOptions();

options.addExtensions(new File("/path/to/extension.crx"));

ChromeDriver driver = new ChromeDriver(options);

Since Selenium version 3.6.0, the ChromeOptions class in Java also implements the Capabilities interface, allowing you to specify other WebDriver capabilities not specific to ChromeDriver.

ChromeOptions options = new ChromeOptions();

// Add the WebDriver proxy capability.

Proxy proxy = new Proxy();

proxy.setHttpProxy("myhttpproxy:3337");

options.setCapability("proxy", proxy);

// Add a ChromeDriver-specific capability.

options.addExtensions(new File("/path/to/extension.crx"));

ChromeDriver driver = new ChromeDriver(options);
Christner answered 20/10, 2021 at 5:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.