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

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

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

Maintaining Team Cohesion in an AI-Powered Workplace: A Step-by-Step GuideGM to Pay $12.75M in Landmark California Settlement Over Secret Sale of Driver Data to InsurersNavigating the Future of Work: New Coursera Programs Build AI, Technical, and Leadership SkillsNavigating AWS's Latest Innovations: A Practical Guide to Amazon Quick, Connect, and OpenAI Partnership in 2026Compose Multiplatform 1.11.0: Enhanced iOS Features, Revamped Testing, and Web Scrolling Improvements