Automating Selenium Driver Setup with WebDriverManager: A Practical Guide

By

Introduction

Automating browser interactions in Java with Selenium is straightforward in theory, but a common roadblock is managing the correct browser driver binaries. Each browser version requires a matching driver, and even a minor mismatch causes runtime errors. Manually downloading, updating, and configuring drivers is tedious and error-prone, especially in team environments or CI/CD pipelines. WebDriverManager solves this by handling driver resolution, download, and setup programmatically. This guide walks you through using WebDriverManager in a Java Selenium project, from adding the dependency to writing driver code that works across different environments.

Automating Selenium Driver Setup with WebDriverManager: A Practical Guide
Source: www.baeldung.com

What You Need

  • A Java development environment (JDK 8 or higher)
  • An IDE (Eclipse, IntelliJ, or VS Code)
  • Maven or Gradle build tool (this guide uses Maven; Gradle steps are also included)
  • Basic familiarity with Selenium WebDriver
  • A browser (Chrome, Firefox, Edge, etc.) installed on your system
  • Internet access (for downloading drivers)

Step-by-Step Instructions

  1. Step 1: Add WebDriverManager as a Dependency

    Include the library in your project’s build file. For Maven, add the following to pom.xml:

    <dependency>
        <groupId>io.github.bonigarcia</groupId>
        <artifactId>webdrivermanager</artifactId>
        <version>6.3.3</version>
        <scope>test</scope>
    </dependency>

    For Gradle, add to build.gradle:

    dependencies {
        testImplementation("io.github.bonigarcia:webdrivermanager:6.3.3")
    }

    Tip: Always use the latest version from Maven Central.

  2. Step 2: Initialize WebDriverManager in Your Code

    Instead of manually setting System.setProperty("webdriver.chrome.driver", ...), use the static method WebDriverManager.chromedriver().setup(). This triggers automatic detection of your Chrome version, download of the matching ChromeDriver binary, and configuration of the system property. Example:

    import io.github.bonigarcia.wdm.WebDriverManager;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    
    public class WebDriverManagerDemo {
        public static void main(String[] args) {
            WebDriverManager.chromedriver().setup();
            WebDriver driver = new ChromeDriver();
            driver.get(\"https://example.com\");
            // ... test actions
            driver.quit();
        }
    }

    Note: This works for Firefox, Edge, Opera, Safari (Tech Preview), and other browsers as well. Use WebDriverManager.driver().setup().

  3. Step 3: Understand Automatic Version Resolution

    WebDriverManager detects the installed browser version by reading system files or registry entries. For example, on Windows it looks in the registry; on Linux/macOS it checks common installation directories or uses the which command. If you want to force a specific driver version, use .driverVersion():

    WebDriverManager.chromedriver().driverVersion(\"114.0.5735.90\").setup();

    This is useful when you need to pin a driver for compatibility.

  4. Step 4: Leverage Driver Caching

    By default, WebDriverManager caches downloaded drivers in ~/.cache/selenium (or equivalent). Subsequent executions reuse the cached binary, speeding up test runs. To clear the cache programmatically:

    WebDriverManager.chromedriver().clearCache();

    You can also change the cache path using .cachePath("/my/custom/path").

  5. Step 5: Integrate with Browsers in Containers

    WebDriverManager supports Dockerized browsers via the .browserInDocker() feature. Example for Chrome in Docker:

    WebDriverManager.chromedriver().browserInDocker().enableVnc().setup();
    WebDriver driver = new ChromeDriver();

    This downloads a Docker image with the browser and driver, starts the container, and connects Selenium. Useful for isolated testing environments.

    Automating Selenium Driver Setup with WebDriverManager: A Practical Guide
    Source: www.baeldung.com
  6. Step 6: Customize Resolution Strategy

    For advanced scenarios (proxies, offline mode, custom binary sources), WebDriverManager provides a fluent API. For example, to avoid downloads entirely if a driver is already present:

    WebDriverManager.chromedriver().avoidAutoResolution().setup();

    To specify a custom download URL (e.g., for a local mirror):

    WebDriverManager.chromedriver().driverUrl(\"https://internal-mirror/chromedriver/\").setup();

    Refer to the official documentation for the full API.

  7. Step 7: Run Tests and Verify

    Execute your test as usual. You should see logs like:

    INFO: Using chromedriver 114.0.5735.90 (resolved from Chrome 114)
    INFO: Exporting webdriver.chrome.driver as /path/to/cache/chromedriver

    If no driver is found locally, WebDriverManager downloads it automatically. The first run may take a few seconds; subsequent runs are faster due to caching.

Tips for Smooth Integration

  • Always check compatibility: WebDriverManager tries to match the exact driver version, but on rare occasions a beta browser may require a beta driver. Use .forceDownload() to retry from the official source.
  • Version pinning in CI/CD: In a pipeline, hardcode the driver version with .driverVersion() to avoid unexpected updates breaking tests.
  • Combine with Selenium Grid: WebDriverManager can be used on each node to ensure drivers are present.
  • Reduce verbosity: Set logging level to WARN during tests to avoid console clutter: WebDriverManager.config().setLogLevel(Level.WARN);
  • Use with headless browsers: Works seamlessly with ChromeOptions().addArguments("--headless").
  • Keep WebDriverManager updated: New browser versions require updated driver mappings. Regularly update the dependency to the latest version.
  • Fallback for offline environments: Download drivers manually and place them in the cache folder. WebDriverManager will skip the download if the binary is present.

By following these steps, you eliminate manual driver management and create a portable Selenium setup that adapts to browser updates automatically. WebDriverManager not only saves time but also reduces test flakiness caused by mismatched driver binaries. Start using it today to streamline your Java Selenium projects.

Tags:

Related Articles

Recommended

Discover More

Persistent Chemicals Found in Some Baby Formulas: What Parents Should KnowUnderstanding Multi-Stage Cyber Attacks: The Epic Boss Battles of CybersecurityTop 10 Highlights of HederaCon 2026 in Miami BeachHow to Decentralize Architecture Decision-Making in Your OrganizationHow Attackers Exploit Amazon SES for Phishing Campaigns: A Step-by-Step Breakdown