Chrome browser implements the WebDriver protocol by using an executable file called ChromeDriver.exe. This executable file starts a server on your system and all your tests will communicate to this server in order to run your tests. In this article, we will learn- How to download the latest version of Selenium ChromeDriver
First, we have to download the latest version of ChromeDriver, mainly because it supports the latest versions of Chrome, and it contains all the bug fixes. The following are the steps to download ChromeDriver.- Step 1: Go to the Chromium official website and download latest version of ChromeDriver based on your operating system
Launching a Chrome driver is easy for launching as any other driver. WebDriver = new ChromeDriver();
package com.journaldev.selenium.Chrome;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class ChromeDriver {
public static void main(String[] args) {
WebDriver driver= new ChromeDriver();
driver.get("https://journaldev.com");
}
}
When you run above program we get an exception called java.lang.IllegalStateException. which tells The path to the driver executable must be set by webdriver.chrome.driver. To overcome the above problem we need to download the ChromeDriver in order to work with selenium commands which we are writing on Chrome. Every browser as a driver. The driver for Chrome is the ChromeDriver. The selenium commands will be interpreted by ChromeDriver and it will be executed on Chrome.
There are 2 methods to initialize ChromeDriver- Use Webdriver.Chrome.Driver
Code to set the System properties is
System.setProperty(“webdriver.chrome.driver”,“Path to chromedriver.exe”);
The complete program to launch the ChromeDriver will be like this:
package com.journaldev.selenium.Chrome;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class ChromeDriver {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","D:\\Drivers\\chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.get("https://journaldev.com");
String PageTitle = driver.getTitle();
System.out.println("Page Title is:" + PageTitle);
driver.close();
}
}
When you run the above program you will notice that Journaldev.com is opened in the new Chrome window and it will print the website title in the console.
Note: Once the path is set you would not need to set the System property every time in the script. Your script will work without the System Property code. The complete program to launch the ChromeDriver will be like this:
package com.journaldev.selenium.Chrome;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromefoxDriver;
public class ChromeDriver {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("https://journaldev.com");
String PageTitle = driver.getTitle();
System.out.println("Page Title is:" + PageTitle);
driver.close();
}
}
When you run the above program your script will work without the System Property code and you will notice that Journaldev.com is opened in the new Chrome window and it will print the website title in the console.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.