Introduction to Browser Drivers in Selenium

Browser Drivers In Selenium

Drivers in Selenium are a vital rather a must for Browser automation. Selenium library in Python or in any other programming language uses the particular Browser’s driver to control browsers with their actions, add functionality to it, and in all manipulate data (webpage) in it.

Contrary to the most common way of declaring a Browser’s Driver which is most common, but often may result in incompatibility issues. There are multiple ways (not so common) to install, initialize web drivers in your Python code.

Recommended read: Python Selenium Introduction and Setup

Downloading the Drivers

The foremost point of error in Selenium occurs when the browser’s driver’s version doesn’t match with the browser’s version, for which compatibility issue arises. So, to start with, you must always use the driver with the same version as that of the Web Browser(Google Chrome, Mozilla Firefox, Apple Safari, or any other) which you intend to use for automation.

Downloading and installing Web Drivers is completely safe because these drivers are maintained by the Official Browser Companies and updated accordingly, for testing and automation purposes of their browser and webpages.

Check with your browser which you intend to automate for its version and download the driver from the below references accordingly:

Web BrowserDriver Download Reference
Google Chrome/ChromiumDownload
Mozilla FirefoxDownload
Microsoft EdgeDownload
Apple SafariAlready built-in
Driver Download Links

Ways to use Drivers

Let’s now look at how we can use the selenium web drivers.

1. Entering the Direct Location

  • The most popular method to use a web driver in Python Selenium code for browser automation. The reason for its popularity is the direct approach it uses, to define a driver in a script.
  • This method requires manual downloading of the web driver.

Advantage: No need to care about lengthy setup or figuring out of Environment variable
Disadvantage: Makes the code less flexible and subject to manual changes for every update

driver = webdriver.Chrome('path/to/chromedriver") 
driver = webdriver.Chrome('C://software/chromedriver.exe') #driver located at the specified location
driver = webdriver.Chrome('chromedriver.exe')  #driver located in the same directory as of the python script file

#other way

service = Service(executable_path="/path/to/chromedriver")
driver = webdriver.Chrome(service=service)

2. Environment Variable

  • If you have tried the above-mentioned method, then you may have noticed how tiring it is to note/remember the location of your installed web driver in some directory of your computer.
  • To tackle for this problem, we define the location or path of our driver in Environment Variable, once and for all. After successful definition, we do not require specifying the path location in our code, and we can code effortlessly.
  • This method also requires manual installation of the driver, as you might have known this method is used for the issue of path variable.

Advantage: No hassle of specifying the path to the driver
Disadvantage: No update functionality

To set the Environment Path Variable, go to your command prompt and type in the following command:In place of “C:\WebDriver\bin” in the below command, use the path (install location) of the driver.

setx PATH "%PATH%;C:\WebDriver\bin"

If you find this way difficult, then you may search for Environment Variable in your Windows Start Menu and click open -“Edit the system environment variables“. After that, from the pop-up window select “Environment Variables” to open another pop-up window.

From the System Variables option, select open Path and now click on New to introduce a new path variable. Paste the location of your web driver in it, then OK, OK, and finally again OK, in all windows.

Environment Variable
Environment Variable

3. Driver Manager

The last and probably the most useful method to use is the Web Driver in your Python code. On selecting, automatic updating in Web Browser, the device only updates the browser and not the installed driver, in this case upon execution of python script the code gives error for not equal versions of the browser and driver.

Advantage: No version compatibility issues and help easily switch between multiple browsers
Disadvantage: Maybe a bit difficult to set up for beginners

Install the driver manager

pip install webdriver-manager

Import the manager

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager            # --> for Chrome

#from webdriver_manager.firefox import GeckoDriverManager               # --> for Firefox


Now as we have installed and imported the manager, we use it in our code like –

Use the install() method to get the location used by the manager and pass it into a loc_service class. This method informs about the location of the installed web driver by itself.

driver = webdriver.Chrome(ChromeDriverManager().install())   # --> for Chrome

#driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())     #--> for Firefox

For any other browser, you may check the official GitHub repository of the driver manager software.

Conclusion

That’s it for the tutorial. Contrary to the popular methods of using the web drivers in Selenium, in this tutorial you learned about the other and maybe advantageous methods to perform the same.