How to use Python WebDriver Manager for Selenium Testing?

Discover how to use Python WebDriver Manager for effortless driver management and seamless Selenium test automation.

Get Started free
Guide Banner Image
Home Guide How to use Python WebDriver Manager for Selenium Testing?

How to use Python WebDriver Manager for Selenium Testing?

Selenium is a powerful tool for automating web browser actions, widely used for testing and web scraping tasks. Traditionally, setting up Selenium required downloading and configuring specific browser drivers, a process that can be time-consuming and error-prone.

The WebDriver Manager simplifies Selenium setup by automatically handling the installation and setup of the appropriate browser drivers, making it easier to get started and maintain Selenium projects.

Overview

What is WebDriver Manager?

WebDriver Manager is a Python library that automates the downloading, installation, and management of browser drivers required for Selenium tests. It ensures seamless compatibility between the browser, driver, and Selenium, eliminating the need for manual driver setup.

Features of WebDriver Manager

  • Automatic Driver Management
  • Compatible with Chrome, Firefox, Edge, and Opera.
  • Version Detection
  • Easy Integration and seamless usage with Selenium.
  • Allows custom driver configurations.

Benefits of WebDriver Manager for Selenium Testing

  • Saves Time: Automates tedious driver setup tasks.
  • Improves Test Reliability: Prevents issues caused by version mismatches.
  • Simplifies Workflow: Reduces maintenance effort for driver management.
  • Enhances Cross-Browser Testing: Streamlines driver handling across browsers.
  • Boosts Productivity: Lets testers focus on writing tests, not setting up drivers.

This article explores how to leverage WebDriver Manager in Python, to streamline Selenium projects by automatically downloading, configuring, and updating the appropriate drivers for your chosen browser.

What is WebDriver Manager for Selenium?

WebDriver Manager is a Python Library that simplifies the setup of Selenium WebDriver by automatically managing browser driver binaries.

Traditionally, Selenium users needed to manually download and configure drivers like ChromeDriver, GeckoDriver, or EdgeDriver, ensuring compatibility with their browser version.

WebDriver Manager automates this process, detecting the browser type and version and downloading the correct driver, reducing setup complexity and minimizing issues related to driver updates. It streamlines Selenium project setup, making it easier and faster to start running tests or automating browser tasks.

How does WebDriver Manager work?

WebDriver Manager is a powerful tool that simplifies browser driver management for Selenium automation projects.

It automates the otherwise tedious tasks of downloading, installing, and updating WebDriver executables, enabling developers to concentrate on writing test scripts rather than handling driver configuration issues.

A brief overview of how it works can be enumerated as below:

  1. Automatically Detects Your Browser Version: WebDriver Manager figures out which browser and version you’re using (like Chrome or Firefox).
  2. Downloads the Right Driver: It finds and downloads the correct driver, based on your browser version(for example, ChromeDriver for Chrome).
  3. Keeps Drivers Updated: When your browser updates, WebDriver Manager will recognize this and download the latest driver, ensuring compatibility.
  4. Streamlines Your Code: Instead of setting up driver paths manually, you can initialize the driver in just one line, making your Selenium scripts simpler and easier to maintain.

Features of WebDriver Manager

The WebDriver Manager is packed with features that make managing browser drivers easy and efficient for Selenium projects.

1. Automatic Driver Detection: It detects your browser and version and then downloads the matching driver automatically.

For example, when using Chrome, WebDriver Manager automatically fetches the latest ChromeDriver if not already available.

from selenium import webdriver

from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())

2. Auto-Updates for Driver Versions: It keeps drivers up to date by checking for the latest compatible driver whenever your browser updates.

For example, If the user updates Firefox, WebDriver Manager will fetch the latest GeckoDriver next time the script is run.

from selenium import webdriver

from webdriver_manager.firefox import GeckoDriverManager

driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())

3. Cross-Browser Support: WebDriver Manager supports a wide range of browsers, including Chrome, Firefox, Edge, Opera, and more.

Each browser has a dedicated manager class that handles driver management seamlessly.

4. Customization Options: WebDriver Manager allows advanced users to specify driver versions, set custom paths, or choose specific mirror URLs for downloading drivers.

This flexibility is especially useful in environments requiring strict control over software versions.

5. Streamlined Integration: It integrates effortlessly with Selenium projects, requiring minimal code changes.

With WebDriver Manager, there’s no need to configure system paths manually or manage driver files explicitly, reducing overhead for developers.

Installing WebDriver Manager for Python

To use WebDriver Manager in Selenium projects, follow these steps to install and configure it.

Prerequisites

1. Python must be installed.

To check, run:

python --version

Or

python3 --version

If not installed, download and install Python from python.org.

2. Python’s package installer, pip, should be installed.

To check, run:

pip --version

Or

pip3 --version

If pip is missing, install it by following these instructions.

3. Selenium must be installed.

WebDriver Manager works alongside Selenium. This can be installed using:

pip install selenium

Steps to Install WebDriver Manager

Follow these step-by-step instructions to install the WebDriver Manager:

1. Install WebDriver Manager

Use pip to install the WebDriver Manager package:

pip install webdriver-manager

Ensure the package is installed successfully by running:

pip show webdriver-manager

2. Set up WebDriver Manager in the Selenium Script

Import and use WebDriver Manager in your Python script. For example, to use ChromeDriver:

from selenium import webdriver

from webdriver_manager.chrome import ChromeDriverManager



driver = webdriver.Chrome(ChromeDriverManager().install())

driver.get("https://www.example.com")

Setting up Selenium with WebDriver Manager

Here are the steps to set up Selenium with WebDriver Manager:

Importing Required Libraries

To begin, you need to import the necessary libraries for Selenium and WebDriver Manager in your Python script:

  • Selenium: To interact with web elements and automate browser actions.
  • WebDriver Manager: To manage and automatically fetch browser drivers.
from selenium import webdriver

from webdriver_manager.chrome import ChromeDriverManager

Initializing WebDriver with WebDriver Manager

WebDriver Manager simplifies the driver initialization process by automatically downloading and configuring the appropriate driver for your browser.

For Chrome, the code should look like this :

driver = webdriver.Chrome(ChromeDriverManager().install())

driver.get("https://www.example.com")

For Firefox,

from webdriver_manager.firefox import GeckoDriverManager

driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())

And, for Edge,

from webdriver_manager.microsoft import EdgeChromiumDriverManager

driver = webdriver.Edge(EdgeChromiumDriverManager().install())

Executing Selenium Tests with WebDriver Manager

To execute Selenium tests with WebDriver Manager, the following steps need to be performed :

Create a Selenium Project

1. Set up a New Python Project

Set Up a New Python Project by creating a directory and initializing it with the necessary files.

mkdir selenium_project

cd selenium_project

2. Install Required Libraries

Use pip to install Selenium and WebDriver Manager.

pip install selenium webdriver-manager

3. Create a Test File

Create a Python file for your tests, for example, test_script.py.

Creating Tests

1. Write the Test Case

Define test logic, including browser automation tasks, such as navigating to a website or verifying page elements. For example,

from selenium import webdriver

from webdriver_manager.chrome import ChromeDriverManager



# Initialize the WebDriver

driver = webdriver.Chrome(ChromeDriverManager().install())



# Open a webpage

driver.get("https://www.example.com")



# Print the title of the page

print("Page Title:", driver.title)



# Close the browser

driver.quit()

2. Organize Tests using a testing framework like unittest or pytest to structure and run multiple test cases efficiently.

Run Selenium Tests on Different Versions of Browsers

With WebDriver Manager, tests can be run on both a specific browser version and multiple versions programmatically. This section will discuss scenarios for Chrome, Firefox, and Edge browsers.

Testing on Specific and Different Versions of Chrome

Steps:

  1. Install the desired Chrome versions or configure the environment to use them.
  2. Specify the ChromeDriver version corresponding to the target Chrome versions using ChromeDriverManager.
from selenium import webdriver

from webdriver_manager.chrome import ChromeDriverManager



# List of ChromeDriver versions for testing different Chrome versions

chrome_versions = ["114.0.5735.90", "115.0.5790.170"]



for version in chrome_versions:

    print(f"Running test on ChromeDriver version: {version}")

    driver = webdriver.Chrome(ChromeDriverManager(version=version).install())

    driver.get("https://www.example.com")

    print(f"ChromeDriver {version} - Page Title:", driver.title)

    driver.quit()

Testing on Specific and Different Versions of Firefox

Steps:

  1. Install the desired Firefox versions or ensure the environment is configured for them.
  2. Use GeckoDriverManager with specific driver versions to match the target Firefox versions.
from selenium import webdriver

from webdriver_manager.firefox import GeckoDriverManager



# List of GeckoDriver versions for testing different Firefox versions

firefox_versions = ["0.30.0", "0.31.0"]



for version in firefox_versions:

    print(f"Running test on GeckoDriver version: {version}")

    driver = webdriver.Firefox(executable_path=GeckoDriverManager(version=version).install())

    driver.get("https://www.example.com")

    print(f"GeckoDriver {version} - Page Title:", driver.title)

    driver.quit()

Testing on Specific and Different Versions of Edge

Steps:

  1. Install the desired Edge versions or configure the environment accordingly.
  2. Use EdgeChromiumDriverManager to fetch specific EdgeDriver versions for testing.
from selenium import webdriver

from webdriver_manager.microsoft import EdgeChromiumDriverManager



# List of EdgeDriver versions for testing different Edge versions

edge_versions = ["114.0.1823.43", "115.0.1901.203"]



for version in edge_versions:

    print(f"Running test on EdgeDriver version: {version}")

    driver = webdriver.Edge(EdgeChromiumDriverManager(version=version).install())

    driver.get("https://www.example.com")

    print(f"EdgeDriver {version} - Page Title:", driver.title)

    driver.quit()

Talk to an Expert

Automating Cross-Browser Testing on Multiple Versions

To run the same test across multiple browsers and versions, the following code snippet can be used :

from selenium import webdriver

from webdriver_manager.chrome import ChromeDriverManager

from webdriver_manager.firefox import GeckoDriverManager

from webdriver_manager.microsoft import EdgeChromiumDriverManager



# Browser and driver version mapping

browsers = {

    "chrome": ["114.0.5735.90", "115.0.5790.170"],

    "firefox": ["0.30.0", "0.31.0"],

    "edge": ["114.0.1823.43", "115.0.1901.203"]

}



for browser, versions in browsers.items():

    for version in versions:

        print(f"Running test on {browser.capitalize()} version: {version}")

        if browser == "chrome":

            driver = webdriver.Chrome(ChromeDriverManager(version=version).install())

        elif browser == "firefox":

            driver = webdriver.Firefox(executable_path=GeckoDriverManager(version=version).install())

        elif browser == "edge":

            driver = webdriver.Edge(EdgeChromiumDriverManager(version=version).install())
        

        driver.get("https://www.example.com")

        print(f"{browser.capitalize()} {version} - Page Title:", driver.title)

        driver.quit()

Pro Tip: Managing multiple browser versions locally can be tedious and time-consuming. With BrowserStack Automate, you can instantly access a cloud-based Grid of real browsers and versions across Chrome, Firefox, Edge, and more. It simplifies cross-browser and cross-version testing without worrying about local setup, ensuring faster and more reliable test execution!

WebDriver Manager vs Selenium Manager

Managing browser drivers is essential for Selenium-based automation testing. Two popular tools for this task are WebDriver Manager and Selenium Manager. While both simplify the process of handling browser drivers, they have distinct features and use cases.

FeatureWebDriver ManagerSelenium Manager
DescriptionA Python library that automates the downloading and management of browser drivers for Selenium tests.A built-in tool in Selenium (from version 4.6) for automatically resolving and managing browser drivers.
InstallationRequires a separate library installation using pip install webdriver-manager.Comes bundled with Selenium. No separate installation is required.
Language SupportSupports Python (via webdriver_manager), Java, .NET, and more through separate implementationsAvailable across all Selenium-supported languages (Java, Python, C#, etc.) as part of Selenium.
Driver CustomizationAllows specifying driver versions, custom download URLs, and cache settings.Supports basic driver resolution with limited configuration options.
Cross-Browser SupportWorks seamlessly with Chrome, Firefox, Edge, Opera, and other browsers.Supports major browsers like Chrome, Firefox, and Edge.
CI/CD CompatibilityFully configurable for CI/CD pipelines and works well with Docker and virtual environments.Simplifies setup for CI/CD but may lack advanced customization.
Version-Specific TestingAllows specifying exact browser driver versions for cross-version testing.Automatically resolves the latest compatible driver version; limited control over specific versions.
Community and UpdatesActively maintained with a large community and regular updates.Part of Selenium’s ecosystem, ensuring consistent updates with Selenium versions.
Ease of UseRequires some additional configuration for advanced use cases.Extremely easy to use for basic setups, as it’s integrated into Selenium.

Uses of Python WebDriver Manager

Python Selenium WebDriver Manager simplifies the management of browser drivers for Selenium-based tests. It automates the process of downloading, updating, and configuring the correct browser drivers, eliminating the need for manual handling and ensuring smooth test execution across various browsers.

  1. Automatic Driver Management: WebDriver Manager automatically detects the browser type and version, downloading and configuring the correct driver for the Selenium WebDriver, ensuring compatibility without manual intervention.
  2. Version Control for Cross-Browser Testing: With Python WebDriver Manager, you can specify particular driver versions to test across multiple browser versions, ensuring your application functions as expected on different browser releases.
  3. Simplifies CI/CD Integration: WebDriver Manager makes it easy to integrate Selenium tests into Continuous Integration (CI) and Continuous Deployment (CD) pipelines by automatically resolving and managing the appropriate driver versions for every run.
  4. Cross-Platform Support: It handles driver management across various operating systems, such as Windows, macOS, and Linux, enabling consistent browser automation regardless of the platform.
  5. Automatic Driver Updates: WebDriver Manager automatically updates browser drivers whenever a new version of the browser is released, ensuring your tests are always running on the latest compatible driver without needing manual updates.

BrowserStack Automate Banner

How BrowserStack helps execute Selenium Tests?

BrowserStack is a cloud-based platform that enables Selenium tests to be executed on real devices and browsers, providing a seamless way to conduct cross-browser and cross-platform testing.

It allows users to run automated tests on a wide variety of environments without requiring local setup or maintenance.

Automate

BrowserStack’s Automate product allows seamless integration with Selenium, enabling automated tests on real browsers in the cloud. This eliminates the need to manage local testing infrastructure and provides access to a variety of browsers, versions, and devices, ensuring comprehensive test coverage.

Check out this documentation on how to Run Selenium with Python on Browserstack Automate.

Selenium Grid

With BrowserStack’s Selenium Grid, users can run tests on multiple browser versions and platforms in parallel. It offers a cloud-hosted grid, enabling tests to be distributed and executed without manual configuration, making parallel test execution efficient and faster.

Integration steps

To integrate Selenium tests with BrowserStack, follow these steps:

  • Sign up on BrowserStack: Create an account and retrieve the username and access key.
  • Install the BrowserStack Python package: Ensure the necessary dependencies are installed.
  • Configure WebDriver: Modify your Selenium script to use BrowserStack’s remote WebDriver, incorporating the desired capabilities, including browser, platform, and version.
  • Execute Tests: Run the tests and analyze the results directly in BrowserStack’s dashboard.

For a more detailed process, please refer to this detailed documentation Integrate Your Test Suite with BrowserStack.

Benefits of executing Selenium tests on Real devices using Browserstack

Running Selenium tests on real devices ensures high-quality, real-world testing. BrowserStack offers the ability to run these tests on actual mobile devices and browsers, providing several key advantages.

  1. Accurate Test Results: Tests are executed on real devices, ensuring results reflect actual user behavior.
  2. Wide Device Coverage: Access to a diverse set of devices, screen sizes, and OS versions for comprehensive testing.
  3. No Device Management: Eliminates the need for physical device procurement, maintenance, and setup.
  4. Faster Time-to-Market: Parallel test execution across multiple real devices accelerates test cycles.
  5. Real User Simulation: Tests run under real network conditions, providing realistic insights into app performance.

Conclusion

WebDriver Manager helps in test automation by eliminating the hassle of manual browser driver management. By automating driver downloads, updates, and compatibility checks, it simplifies the Selenium setup process, saving valuable time and ensuring smoother testing workflows.

Additionally, leveraging BrowserStack Automate for Selenium test execution offers the benefits of running tests on real devices and a wide array of browsers, ensuring more accurate and comprehensive testing.

Together, these tools streamline the testing process and ensure cross-browser compatibility, ultimately accelerating development and improving test coverage.

Try BrowserStack Now

Tags
Automation Testing Website Testing