Reputation: 33
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
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:
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:
Chrome, Firefox
).chromedriver
, geckodriver
). If unknown, it uses the latest version of the driver.WebDriverManager
cache (~/.cache/selenium by default).
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