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:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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
OptionsandServicearguments 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:UsersAdmin.wdmdriverschromedriverwin3296.0.4664.45chromedriver.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
Method 2
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')
Method 3
This works for me
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
ser = Service("C:\chromedriver.exe")
op = webdriver.ChromeOptions()
s = webdriver.Chrome(service=ser, options=op)
Method 4
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)
Method 5
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())
Method 6
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
Method 7
if you are using any IDE like PyCharm install webdriver-manager package of that IDE as how do install for selenium package
Method 8
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);
Method 9
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
service_obj = Service("WebDrivers_pathchromedriver.exe")
driver = webdriver.Chrome(service=service_obj)
driver.get("https://www.google.com")
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0