2

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

Michael Mintz
  • 9,007
  • 6
  • 31
  • 48
Hemendra
  • 81
  • 8
  • If this helps - [Selenium Manager (Beta)](https://www.selenium.dev/documentation/selenium_manager/) – Shawn Aug 02 '23 at 12:05

3 Answers3

2

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)
Guy
  • 46,488
  • 10
  • 44
  • 88
  • Thank you for the details, but i was asking something else here. the `executable_path` argument above is used when you already have the webdriver downloaded in that folder. I want to know if there is a way to tell Selenium Manager to set the driver download directory to some other folder (possibly inside the project folder, instead of the .cache in Users directory. For reference, when we use webdriver_manager, we can provide `path` argumenet with the desired download path. `chrome_Service(ChromeDriverManager(path = "./Drivers", cache_valid_range=10).install())` – Hemendra Aug 02 '23 at 15:28
  • Nevermind, I went through the selenium package code and found the answer: `from selenium.webdriver.common.driver_finder import DriverFinder chrome_options = Options() driver_path = DriverFinder.get_path(Service(), chrome_options)` This gives you the download path as `C:\Users\xxxxxx\.cache\selenium\chromedriver\win64\114.0.5735.90\chromedriver.exe` – Hemendra Aug 02 '23 at 15:39
0

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

Shawn
  • 4,064
  • 2
  • 11
  • 23
0

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()
Michael Mintz
  • 9,007
  • 6
  • 31
  • 48