Python: How to set a custom webdriver download path for the built-in Selenium Manager

1.2k Views Asked by At

How to set a custom webdriver download path for the built-in Selenium Manager? Unlike other third-party driver managers, there is very little documentation available for this, and I could not find any related discussion anywhere.

driver = webdriver.Chrome(options=chrome_options)

Does the WebDriver object have the driver-path data in it which can be extracted? <selenium.webdriver.chrome.webdriver.WebDriver (session="8cc15b28be77b3773576ef8b373be420")>

5

There are 5 best solutions below

2
Guy On

webdriver.Chrome has an optional service parameter

def __init__(self, options: Options = None, service: Service = None, keep_alive: bool = True):

    service = service if service else Service()
    options = options if options else Options()

    super().__init__(...)

If it's None it creates a default Service

class Service(service.ChromiumService):

    def __init__(self, executable_path=None, ...):
        super().__init__(executable_path=executable_path, ...)

You can fill it with your own Service

webdriver.Chrome(service=Service(executable_path=driver_executable_path), options=options)
0
Shawn On

If this is what you are looking for!

  • Below code uses the chromedriver.exe from the path provided in Service class
from selenium import webdriver
from selenium.webdriver.chrome.service import Service

service = Service('C:\Backup\Selenium jars\drivers\chromedriver-win32\chromedriver.exe') # chromedriver.exe path
driver = webdriver.Chrome(service = service)
driver.get("https://www.google.com/")
  • Below code uses the chromedriver.exe which is downloaded/managed by SeleniumManager
from selenium import webdriver

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

SeleniumManager will look for/ or download the chromedriver.exe in below path:

enter image description here

0
Michael Mintz On

To pass in an executable_path, you can use the service arg.

Here's an example for Chrome:

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

service = Service(executable_path="./chromedriver")
options = webdriver.ChromeOptions()
options.add_argument('--headless')
driver = webdriver.Chrome(service=service, options=options)
# ...
driver.quit()

Here's an example for Firefox:

from selenium import webdriver
from selenium.webdriver.firefox.service import Service

service = Service(executable_path="PATH_TO_GECKODRIVER")
options = webdriver.FirefoxOptions()
driver = webdriver.Firefox(service=service, options=options)
# ...
driver.quit()

You no longer need to specify an executable_path due to a fully operational Selenium Manager in 4.11.2, so this is all you need:

from selenium import webdriver
driver = webdriver.Chrome()
# ...
driver.quit()

Here's one with the placeholders for options / preferences:

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

service = Service()
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)
# ...
driver.quit()
0
kyrlon On

TL;DR Answer

Set the environment variable SE_CACHE_PATH to the desired drivers path: os.environ["SE_CACHE_PATH"] = "/path/to/my/drivers/folder"

Complete Answer

You will need to modify the cache directory for Selenium Manager if you want to alter the location from which it downloads drivers. As far as I can tell, nothing is as nice as WebDriveManager as mentioned in this post, which conveniently sets the path. The rust executable for Selenium Manager is wrapped in the selenium_manager.py file. The feature to change the default cache directory was requested in a PR that was merged soon after this question was posted. Other than editing the source code for selenium_manager.py to add the --cache-path flag to the args variable with the path following, there is no convenient way to change the cache directory with an exposed method/function at this time. The environment variable SE_CACHE_PATH should be set as shown below for the best method:

from pathlib import Path
import os
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options

DRIVERS_DIR = Path(__file__).resolve().parent / "drivers"
DRIVERS_DIR.mkdir(exist_ok=True)

os.environ["SE_CACHE_PATH"] = DRIVERS_DIR.name
        
driver = webdriver.Chrome(service=Service())

if __name__ == "__main__":
    driver.get("https://stackoverflow.com/")
1
Pranesh Sthapit On

None of the above worked me so I went into the package code and finally figure out the solution. Hope this will help someone.

from webdriver_manager.core.driver_cache import DriverCacheManager

driver_path = r"C:\MyPath"

driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager(cache_manager=DriverCacheManager(driver_path)).install()), options=options)