Package: webdriver-manager
Webdriver Manager for PythonSupport the library onPatreonThe main idea is to simplify management of binary drivers for different browsers.For now support:ChromeDriverEdgeChromiumDriverGeckoDriverIEDriverOperaDriverCompatible with Selenium 4.x and below.Before:
You need to download the chromedriver binary, unzip it somewhere on your PC and set the path to this driver like this:fromseleniumimportwebdriverdriver=webdriver.Chrome('/home/user/drivers/chromedriver')It’s boring!!! Moreover, every time a new version of the driver is released, you need to repeat all these steps again and again.With webdriver manager, you just need to do two simple steps:Install manager:pipinstallwebdriver-managerUse with Chrome# selenium 3fromseleniumimportwebdriverfromwebdriver_manager.chromeimportChromeDriverManagerdriver=webdriver.Chrome(ChromeDriverManager().install())# selenium 4fromseleniumimportwebdriverfromselenium.webdriver.chrome.serviceimportServiceasChromeServicefromwebdriver_manager.chromeimportChromeDriverManagerdriver=webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))Use with Chromium# selenium 3fromseleniumimportwebdriverfromwebdriver_manager.chromeimportChromeDriverManagerfromwebdriver_manager.core.os_managerimportChromeTypedriver=webdriver.Chrome(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install())# selenium 4fromseleniumimportwebdriverfromselenium.webdriver.chrome.serviceimportServiceasChromiumServicefromwebdriver_manager.chromeimportChromeDriverManagerfromwebdriver_manager.core.os_managerimportChromeTypedriver=webdriver.Chrome(service=ChromiumService(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install()))Use with Brave# selenium 3fromseleniumimportwebdriverfromwebdriver_manager.chromeimportChromeDriverManagerfromwebdriver_manager.core.os_managerimportChromeTypedriver=webdriver.Chrome(ChromeDriverManager(chrome_type=ChromeType.BRAVE).install())# selenium 4fromseleniumimportwebdriverfromselenium.webdriver.chrome.serviceimportServiceasBraveServicefromwebdriver_manager.chromeimportChromeDriverManagerfromwebdriver_manager.core.os_managerimportChromeTypedriver=webdriver.Chrome(service=BraveService(ChromeDriverManager(chrome_type=ChromeType.BRAVE).install()))Use with Edge# selenium 3fromseleniumimportwebdriverfromwebdriver_manager.microsoftimportEdgeChromiumDriverManagerdriver=webdriver.Edge(EdgeChromiumDriverManager().install())# selenium 4fromseleniumimportwebdriverfromselenium.webdriver.edge.serviceimportServiceasEdgeServicefromwebdriver_manager.microsoftimportEdgeChromiumDriverManagerdriver=webdriver.Edge(service=EdgeService(EdgeChromiumDriverManager().install()))Use with Firefox# selenium 3fromseleniumimportwebdriverfromwebdriver_manager.firefoximportGeckoDriverManagerdriver=webdriver.Firefox(executable_path=GeckoDriverManager().install())# selenium 4fromseleniumimportwebdriverfromselenium.webdriver.firefox.serviceimportServiceasFirefoxServicefromwebdriver_manager.firefoximportGeckoDriverManagerdriver=webdriver.Firefox(service=FirefoxService(GeckoDriverManager().install()))Use with IE# selenium 3fromseleniumimportwebdriverfromwebdriver_manager.microsoftimportIEDriverManagerdriver=webdriver.Ie(IEDriverManager().install())# selenium 4fromseleniumimportwebdriverfromselenium.webdriver.ie.serviceimportServiceasIEServicefromwebdriver_manager.microsoftimportIEDriverManagerdriver=webdriver.Ie(service=IEService(IEDriverManager().install()))Use with Opera# selenium 3fromseleniumimportwebdriverfromselenium.webdriver.chromeimportservicefromwebdriver_manager.operaimportOperaDriverManagerwebdriver_service=service.Service(OperaDriverManager().install())webdriver_service.start()driver=webdriver.Remote(webdriver_service.service_url,webdriver.DesiredCapabilities.OPERA)# selenium 4fromseleniumimportwebdriverfromselenium.webdriver.chromeimportservicefromwebdriver_manager.operaimportOperaDriverManagerwebdriver_service=service.Service(OperaDriverManager().install())webdriver_service.start()options=webdriver.ChromeOptions()options.add_experimental_option('w3c',True)driver=webdriver.Remote(webdriver_service.service_url,options=options)If the Opera browser is installed in a location other thanC:/Program FilesorC:/Program Files (x86)on Windows
and/usr/bin/operafor all unix variants and mac, then use the below code,options=webdriver.ChromeOptions()options.binary_location="path/to/opera.exe"driver=webdriver.Remote(webdriver_service.service_url,options=options)Get browser version from pathTo get the version of the browser from the executable of the browser itself:fromwebdriver_manager.firefoximportGeckoDriverManagerfromwebdriver_manager.core.utilsimportread_version_from_cmdfromwebdriver_manager.core.os_managerimportPATTERNversion=read_version_from_cmd("/usr/bin/firefox-bin --version",PATTERN["firefox"])driver_binary=GeckoDriverManager(version=version).install()Custom Cache, File manager and OS Managerfromwebdriver_manager.chromeimportChromeDriverManagerfromwebdriver_manager.core.file_managerimportFileManagerfromwebdriver_manager.core.driver_cacheimportDriverCacheManagerfromwebdriver_manager.core.os_managerimportOperationSystemManagercache_manager=DriverCacheManager(file_manager=FileManager(os_system_manager=OperationSystemManager()))manager=ChromeDriverManager(cache_manager=cache_manager)os_manager=OperationSystemManager(os_type="win64")Configurationwebdriver_managerhas several configuration variables you can be interested in.
Any variable can be set using either .env file or via python directlyGH_TOKENwebdriver_managerdownloading some webdrivers from their official GitHub repositories but GitHub haslimitationslike 60 requests per hour for unauthenticated users.
In case not to face an error related to GitHub credentials, you need tocreateGitHub token and place it into your environment: (*)Example:exportGH_TOKEN="asdasdasdasd"(*) access_token required to work with GitHub APImore info.There is also possibility to set same variable via ENV VARIABLES, example:importosos.environ['GH_TOKEN']="asdasdasdasd"WDM_LOGTurn off webdriver-manager logs use:importloggingimportosos.environ['WDM_LOG']=str(logging.NOTSET)WDM_LOCALBy default, all driver binaries are saved to user.home/.wdm folder. You can override this setting and save binaries to project.root/.wdm.importosos.environ['WDM_LOCAL']='1'WDM_SSL_VERIFYSSL verification can be disabled for downloading webdriver binaries in case when you have troubles with SSL Certificates or SSL Certificate Chain. Just set the environment variableWDM_SSL_VERIFYto"0".importosos.environ['WDM_SSL_VERIFY']='0'versionSpecify the version of webdriver you need. And webdriver-manager will download it from sources for your os.fromwebdriver_manager.chromeimportChromeDriverManagerChromeDriverManager(driver_version="2.26").install()cache_valid_rangeDriver cache by default is valid for 1 day. You are able to change this value using constructor parameter:fromwebdriver_manager.chromeimportChromeDriverManagerfromwebdriver_manager.core.driver_cacheimportDriverCacheManagerChromeDriverManager("2.26",cache_manager=DriverCacheManager(valid_range=1)).install()os_typeFor some reasons you may use custom OS/arch. You are able to change this value using constructor parameter:fromwebdriver_manager.chromeimportChromeDriverManagerfromwebdriver_manager.core.os_managerimportOperationSystemManagerChromeDriverManager(os_system_manager=OperationSystemManager(os_type="linux-mips64")).install()urlYou may use any other repo with drivers and release URl. You are able to change this value using constructor parameters:fromwebdriver_manager.chromeimportChromeDriverManagerChromeDriverManager(url="https://custom-repo.url",latest_release_url="https://custom-repo.url/LATEST").install()Custom LoggerIf you need to use a custom logger, you can create a logger and set it withset_logger().importloggingfromwebdriver_manager.core.loggerimportset_loggerlogger=logging.getLogger("custom_logger")logger.setLevel(logging.DEBUG)logger.addHandler(logging.StreamHandler())logger.addHandler(logging.FileHandler("custom.log"))set_logger(logger)Custom HTTP ClientIf you need to add custom HTTP logic like session or proxy you can define your custom HttpClient implementation.importosimportrequestsfromrequestsimportResponsefromwebdriver_manager.chromeimportChromeDriverManagerfromwebdriver_manager.core.download_managerimportWDMDownloadManagerfromwebdriver_manager.core.httpimportHttpClientfromwebdriver_manager.core.loggerimportlogclassCustomHttpClient(HttpClient):defget(self,url,params=None,**kwargs)->Response:"""Add you own logic here like session or proxy etc."""log("The call will be done with custom HTTP client")returnrequests.get(url,params,**kwargs)deftest_can_get_chrome_driver_with_custom_http_client():http_client=CustomHttpClient()download_manager=WDMDownloadManager(http_client)path=ChromeDriverManager(download_manager=download_manager).install()assertos.path.exists(path)This will make your test automation more elegant and robust!Cheers