The main idea is to simplify management of binary drivers for different browsers.
For now support:
ChromeDriver
GeckoDriver
IEDriver
OperaDriver
EdgeChromiumDriver
Before: You should download binary chromedriver, unzip it somewhere in you PC and set path to this driver like this:
fromseleniumimportwebdriverdriver=webdriver.Chrome('/home/user/drivers/chromedriver')It’s boring!!! Moreover every time the new version of driver released, you should go and repeat all steps again and again.
With webdriver manager, you just need to do two simple steps:
Install manager:
pip install webdriver-managerUse with Chrome:
fromseleniumimportwebdriverfromwebdriver_manager.chromeimportChromeDriverManagerdriver=webdriver.Chrome(ChromeDriverManager().install())Use with Chromium:
fromseleniumimportwebdriverfromwebdriver_manager.chromeimportChromeDriverManagerfromwebdriver_manager.utilsimportChromeTypedriver=webdriver.Chrome(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install())Use with FireFox:
fromseleniumimportwebdriverfromwebdriver_manager.firefoximportGeckoDriverManagerdriver=webdriver.Firefox(executable_path=GeckoDriverManager().install())Use with IE
fromseleniumimportwebdriverfromwebdriver_manager.microsoftimportIEDriverManagerdriver=webdriver.Ie(IEDriverManager().install())Use with Edge
fromseleniumimportwebdriverfromwebdriver_manager.microsoftimportEdgeChromiumDriverManagerdriver=webdriver.Edge(EdgeChromiumDriverManager().install())Use with Opera
fromseleniumimportwebdriverfromwebdriver_manager.operaimportOperaDriverManagerdriver=webdriver.Opera(executable_path=OperaDriverManager().install())If the Opera browser is installed in a location other than C:/Program Files or C:/Program Files (x86) on windows and /usr/bin/opera for all unix variants and mac, then use the below code,
fromseleniumimportwebdriverfromwebdriver_manager.operaimportOperaDriverManageroptions=webdriver.ChromeOptions()
options.add_argument('allow-elevated-browser')
options.binary_location="C:\\Users\\USERNAME\\FOLDERLOCATION\\Opera\\VERSION\\opera.exe"driver=webdriver.Opera(executable_path=OperaDriverManager().install(), options=options)webdriver_manager has several configuration variables you can be interested in.
webdriver_manager downloading some webdrivers from their official GitHub repositories but GitHub has limitations like 60 requests per hour for unauthenticated users. In case not to face an error related to github credentials, you need to create github token and place it into your environment: (*)
Example:
# bashexport GH_TOKEN = "asdasdasdasd"(*) access_token required to work with GitHub API more info.
There is also possibility to set same variable via ENV VARIABLES, example:
# pythonimportosos.environ['GH_TOKEN'] ="asdasdasdasd"To silent webdriver_manager logs and remove them from console, initialize env variable WDM_LOG_LEVEL with '0' value before your selenium tests:
importosos.environ['WDM_LOG_LEVEL'] ='0'or via constructor:
ChromeDriverManager("2.26", log_level=0).install()By default webdriver manager prints a blank space before its log output if logging is enabled. If you want to disable this, initialize WDM_PRINT_FIRST_LINE with 'False' before your tests:
importosos.environ['WDM_PRINT_FIRST_LINE'] ='False'or via constructor:
ChromeDriverManager("2.26", print_first_line=False).install()By default all driver binaries are saved to user.home/.wdm folder. You can override this setting and save binaries to project.root/.wdm.
import os
os.environ['WDM_LOCAL'] = '1'
Driver cache by default is valid for 1 day. You are able to change this value using constructor parameter:
ChromeDriverManager("2.26", cache_valid_range=1).install()This will make your test automation more elegant and robust!
Cheers