Paul Recker
Paul Recker

Reputation: 33

How to autoinstall the right version of chromedriver for Selenium Java

I am trying to build a webscraper for a specific WebApp using Selenium. Since I do not always want to update my Java programm i need to find a solution in which i can auto-install the newest version of chromedriver when i start the programm. I could only find solutions for python.

Upvotes: 2

Views: 3662

Answers (1)

cruisepandey
cruisepandey

Reputation: 29362

WebDriverManager is a library which allows to automate the management of the drivers (e.g. chromedriver, geckodriver, etc.) required by Selenium WebDriver.

If you use Selenium WebDriver, you probably know that to use some browsers such as Chrome, Firefox, Edge, Opera, PhantomJS, or Internet Explorer, first you need to download the so-called driver, i.e. a binary file which allows WebDriver to handle these browsers. In Java, the path to this driver should be set as JVM properties, as follows:

System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");

This is quite annoying since it forces you to link directly this driver into your source code. In addition, you have to check manually when new versions of the drivers are released. WebDriverManager comes to the rescue, performing in an automated way this job for you. WebDriverManager can be used in different ways:

WebDriverManager as Java dependency

To use WebDriverManager from tests in a Maven project, you need to add the following dependency in your pom.xml (Java 8 or upper required), typically using the test scope:

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

Once we have included this dependency, you can let WebDriverManager to do the driver management for you.

public class ChromeTest {

    private WebDriver driver;

    @BeforeClass
    public static void setupClass() {
        WebDriverManager.chromedriver().setup();
    }

    @Before
    public void setupTest() {
        driver = new ChromeDriver();
    }

    @After
    public void teardown() {
        if (driver != null) {
            driver.quit();
        }
    }

    @Test
    public void test() {
        // Your test code here
    }

}

Notice that simply adding WebDriverManager.chromedriver().setup(); WebDriverManager does magic for you:

  1. It checks the version of the browser installed in your machine (e.g. Chrome, Firefox).
  2. It matches the version of the driver (e.g. chromedriver, geckodriver). If unknown, it uses the latest version of the driver.
  3. It downloads the driver if it is not present on the WebDriverManager cache (~/.cache/selenium by default).
  4. It exports the proper WebDriver Java environment variables required by Selenium (not done when using WebDriverManager from the CLI or as a Server).

WebDriverManager resolves the drivers for the browsers Chrome, Firefox, Edge, Opera, PhantomJS, Internet Explorer, and Chromium. For that, it provides several drivers managers for these browsers. These drivers managers can be used as follows:

WebDriverManager.chromedriver().setup();
WebDriverManager.firefoxdriver().setup();
WebDriverManager.edgedriver().setup();
WebDriverManager.operadriver().setup();
WebDriverManager.phantomjs().setup();
WebDriverManager.iedriver().setup();
WebDriverManager.chromiumdriver().setup();

Upvotes: 4

Related Questions