Selenium Automatically Get Driver

Profile picture for user arilio666

Let us discuss how we can automatically update the driver on the go and proceed to the test without manual effort.

  • Selenium WebDriver provides a way to automatically locate and download the appropriate driver executable for your browser using the WebDriverManager library.
  • WebDriverManager is a Java library that allows you to automatically manage the binary versions of different web drivers. It is an open-source project and can be used to avoid the manual steps of downloading and configuring the web drivers. This is especially useful when running tests on different platforms and different browsers.
  • To use WebDriverManager, you must add the library to your project's classpath and import the relevant classes. Here's an example of how to use WebDriverManager to download and configure the ChromeDriver executable automatically:
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Example {
   public static void main(String[] args) {
       // Configure and download ChromeDriver
       WebDriverManager.chromedriver().setup();
       // Create a new instance of ChromeDriver
       WebDriver driver = new ChromeDriver();
       // Use the driver as usual
       driver.get("http://www.google.com");
       System.out.println(driver.getTitle());
       // Close the browser
       driver.quit();
   }
}
  • This will automatically download the appropriate version of ChromeDriver and configure it for you.

Note that the WebDriverManager must be added to the project dependencies. If you are using maven, you can add the following to your pom.xml file:

<dependency>
   <groupId>io.github.bonigarcia</groupId>
   <artifactId>webdrivermanager</artifactId>
   <version>3.9.0</version>
   <scope>test</scope>
</dependency>
  • WebDriverManager also supports other browsers such as firefox, internet explorer, opera, safari, etc. You can also configure the version and architecture of the driver. Refer to their documentation for more details.