Table of Contents
URL: https://www.progressiverobot.com/running-test-on-selenium-chrome-driver/
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
- How to setup Selenium ChromeDriver in multiple ways
Download 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
_Note: Here we are working on Windows Operating system, we need to download the corresponding Chrome driver of Windows version. If your Operating System is Linux or Mac then you need to download the corresponding Chrome driver._
- Step 2: Click on _ChromeDriver 73.0.3683.20_ link. You will be navigated to ChromeDriver download page which contains ChromeDriver for Linux, Mac and Windows operating systems.
- Step 3: Click on _chromedriver_win32.zip_ to download ChromeDriver for Windows.
- Step 4: Once the zip file is downloaded, you can unzip it to retrieve _chromedriver.exe._ Note the location where you extracted the ChromeDriver. Location will be later used to instantiate the driver.
Launching Chrome Browser using Selenium WebDriver
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.
Different ways to initialize ChromeDriver
There are 2 methods to initialize ChromeDriver- Use Webdriver.Chrome.Driver
- Use Environment Variables
_Method 1: Use Webdriver.chrome.driver system property_
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.
_Method 2: Setting ChromeDriver Path in Windows Environment Variables_
- Step 1: Go to _My Computer_ and _Right click_ to get the context menu.
- Step 2: Click on the _Change Setting_ on the opened window.
- Step 3: Click on _Advance_ tab and click on _Environment Variables._
- Step 4: Select _Path_ under _System Variables_ and click on _Edit._
- Step 5: At the end of the string use _semicolon_ and paste the path of the _ChromeDriver._ On my machine my ChromeDriver exe resides in _D:\Drivers\_
_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.