Executing Selenium WebDriver Script with Chrome Driver

In this tutorial, we will cover how to execute Selenium scripts on Google Chrome browser. Unlike Mozilla Firefox Driver, Selenium does not have an inbuilt support of chrome browser. To initialize chrome browser an executable file named chromedriver.exe is required.

Environment Setup for Chrome Browser:

Visit the link Chrome Driver Executable

To download chrome driver executable, visit third party section of download section seleniumhq.org website. Refer screenshots for ease:

Download Chrome Driver

 

Download the latest version of chrome driver executable (at the time this article is written its 2.22).

Let us use this executable in our code:

To invoke chrome browser:

  1. Create an instance of ChromeDriver class.
  2. Set the system property of the variable “webdriver.chrome.drive” and update the value with the path of chromedriver executable file.
  3. Initialize the ChromeDriver class.
ChromeDriver driver;

System.setProperty("webdriver.chrome.driver","<path of executable>\\chromedriver.exe");
		
driver = new ChromeDriver();

setProperty(“key”, “value”) method is used in Java to set the property (value of the system variable), it accepts two string arguments i.e. variable name (here, its “webdriver.chrome.driver”) and its value (the location where driver.exe is placed). Give exact driver name and full driver location.

Points to Remember: If we don’t set the path / or if we give the wrong path, then you will be seeing below error immediately once you start your script.

Error: The path to the driver executable must be set by the webdriver.chrome.driver system property

Now we are good to start working with chrome driver. All other basic APIs will remains same in the case of Chrome browser also. For API walkthrough read Working with FirefoxDriver.

package com.qatechhub.day1;

import org.openqa.selenium.chrome.ChromeDriver;

public class WorkingWithChromeDriver {

ChromeDriver ChromeDriver;

String url = "http://www.qatechhub.com";

public void invokeBrowser(){
/*Setting property of a system variable used by chrome driver*/
System.setProperty("webDriver.chrome.driver", "C:\\workspace\\libs\\chromedriver.exe");

ChromeDriver = new ChromeDriver();

/* To Maximize the window */
ChromeDriver.manage().window().maximize();

/* To delete all the cookies of the browser */
ChromeDriver.manage().deleteAllCookies();

/* To navigate to a particular URL */
ChromeDriver.get(url);

String titleOfPage;

/* To get The Title of the page */

titleOfPage = ChromeDriver.getTitle();

System.out.println("Title of the page is : " + titleOfPage);

String currentUrl;

/* To get The url of the page */
currentUrl = ChromeDriver.getCurrentUrl();

System.err.println("Title of the page is : "+ currentUrl);

String pageSource;

/* To get The source code current page */
pageSource = ChromeDriver.getPageSource();

System.out.println("Page Source : "+ pageSource);

}

public void closeBrowser(){
/* To close current active window of the browser */
ChromeDriver.close();
}

public void closeAllBrowser(){

/* To close all the opened browser session*/
ChromeDriver.quit();
}
}

In the next tutorial, we will cover How to execute selenium scripts with Internet Explorer and what are challenges faced with Internet Explorer.

PS: For any questions, queries or feedback. Feel free to write us at amrita@qatechhub.com or support@qatechhub.com Happy learning 🙂

Amrita Joshi

About the Author

Amrita Joshi