Package: hf-webdriver-manager
Webdriver Manager for PythonThis is a fork project based onSergeyPirogov/webdriver_manager,
which is using a daily refreshed mirror of all the driver
sitesHansBug/browser_drivers_mirror. This means this package
can
be directly used in China mainland.Support 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:pipinstallhf-webdriver-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.utilsimportChromeTypedriver=webdriver.Chrome(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install())# selenium 4fromseleniumimportwebdriverfromselenium.webdriver.chrome.serviceimportServiceasChromiumServicefromwebdriver_manager.chromeimportChromeDriverManagerfromwebdriver_manager.core.utilsimportChromeTypedriver=webdriver.Chrome(service=ChromiumService(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install()))Use with Brave# selenium 3fromseleniumimportwebdriverfromwebdriver_manager.chromeimportChromeDriverManagerfromwebdriver_manager.core.utilsimportChromeTypedriver=webdriver.Chrome(ChromeDriverManager(chrome_type=ChromeType.BRAVE).install())# selenium 4fromseleniumimportwebdriverfromselenium.webdriver.chrome.serviceimportServiceasBraveServicefromwebdriver_manager.chromeimportChromeDriverManagerfromwebdriver_manager.core.utilsimportChromeTypedriver=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.core.utilsimportread_version_from_cmd,PATTERNversion=read_version_from_cmd("/usr/bin/firefox-bin --version",PATTERN["firefox"])driver_binary=FirefoxDriverManager(version=version).install()Configurationwebdriver_managerhas several configuration variables you can be interested in.
Any variable can be set using either .env file or via python directlyDRIVER_SITEYou can change the resource address of the Selenium browser driver by setting theDRIVER_SITEenvironment variable.
The default value ishttps://huggingface.co/datasets/HansBug/browser_drivers_mirror/resolve/main.WDM_LOGTurn off hf-webdriver-manager logs use:importloggingimportosos.environ['WDM_LOG']=str(logging.NOTSET)WDM_PROGRESS_BARTurn off the progress bar which is displayed on downloads:importosos.environ['WDM_PROGRESS_BAR']=str(0)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'pathSet the directory where you want to download and save the webdriver. You can use relative and absolute paths.fromwebdriver_manager.chromeimportChromeDriverManagerChromeDriverManager(path=r".\\Drivers").install()versionSpecify the version of webdriver you need. And hf-webdriver-manager will download it from sources for your os.fromwebdriver_manager.chromeimportChromeDriverManagerChromeDriverManager(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.chromeimportChromeDriverManagerChromeDriverManager("2.26",cache_valid_range=1).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