Browser drivers with Python Webdriver Manager
We all know that we need to have browser drivers, .exe files like chromedriver.exe and geckodriver.exe in case of windows environment or binary files like chromedriver and gheckodriver in case of linux distributions, in order to run our selenium webdriver automation scripts on chrome and firefox browsers.
And also we need to set the path of these files in our script like below or need to add location to the class path.
driver = webdriver.Chrome(executable_path="/path/to/binary/chromedriver")
driver = webdriver.Firefox(executable_path="/path/to/binary/firefoxdriver")
If the path is not defined/given or if the path provided is wrong, we will get an exceptions. And it is mandatory for all browsers to have browser drivers.
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
To avoid this error, we need to manually download and manage these browser drivers for each operating systems/environments which is very painful and time consuming. Above all we also have to check and update relevant drivers when new versions of the binaries are released or new browsers versions are released along with compatibility for driver to browser.
webdriver_manager by Sergey Pirogov helps us to manage driver related settings automatically for python. This package downloads binaries/executables in an automated way and helps us to avoid all the manual steps that we do previously related to drivers to run our tests.
It supports browsers such as Chrome, Firefox, PhantomJS, Microsoft Edge, or Internet Explorer. You can check that in project page here.
How do we use this in our project?
Only setup required is to install this package using ‘pip’.
pip install webdriver_manager
That’s it! We are all set. Just import this module in your python project and start using it.
from webdriver_manager.chrome import ChromeDriverManager from selenium import webdriver driver = webdriver.Chrome(executable_path=ChromeDriverManager().install()) driver.get("http://www.google.com/") print(driver.title) driver.quit()
from webdriver_manager.firefox import GeckoDriverManager from selenium import webdriver driver = webdriver.Firefox(executable_path=GeckoDriverManager().install()) driver.get("http://www.google.com/") print(driver.title) driver.quit()
from webdriver_manager.microsoft import EdgeDriverManager from selenium import webdriver driver = webdriver.Edge(executable_path=EdgeDriverManager().install()) driver.get("http://www.google.com/") print(driver.title) driver.quit()
from webdriver_manager.microsoft import IEDriverManager from selenium import webdriver driver = webdriver.Ie(executable_path=IEDriverManager().install()) driver.get("http://www.google.com/") print(driver.title) driver.quit()
webdriver_manager, by default, tries to download the latest version of a given driver binary. To use a specific version of driver, pass the driver version like below.
This eliminates lot of manual works and incompatibility issues. Thanks to Sergey Pirogov. Happy scripting!!!
Don’t forget to provide your valuable comments.