Tutorial

Running Test on Selenium Chrome Driver

Published on August 4, 2022
author

Meghna Gangwar

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

Chrome Version
Chrome Version
  • 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. 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.
Chrome Index
Chrome Index
  • 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.
Chrome Driver
Chrome 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.
MyComputer Properties
MyComputer Properties
  • Step 2: Click on the Change Setting on the opened window.
Change Settings
Change Settings
  • Step 3: Click on Advance tab and click on Environment Variables.
System Properties
System Properties
  • Step 4: Select Path under System Variables and click on Edit.
Environment Variables
Environment Variables
  • 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\
Path
Path

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.

Learn more about our products

About the authors
Default avatar
Meghna Gangwar

author

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.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.