How to execute WebDriver Script in Chrome Browser ?

In order to execute selenium script on Chrome browser, first you have to download the ChromeDriver server.

ChromeDriver is supported by the Google Chromium team and it can be downloaded from https://sites.google.com/a/chromium.org/chromedriver/

For windows environment, you will have to download the following version : chromedriver_win32.zip

1

As ChromeDriver is a separate executable file which selenium uses to communicate with Chrome browser.

Selenium code interacts with ChromeDriver server through JSON wire Protocol and then ChromeDriver will further communicate with chrome browser. So basically, chrome driver will act as a bridge between selenium code and Chrome Browser. Just have a look at below picture :

1

Command to setup ChromeDriver path in your selenium code

System.setProperty("webdriver.chrome.driver", "chromedriverpath/chromedriver.exe");

And if you try to execute you selenium code on Chrome Browser without setting up the path of ChromeDriver Server in your code, you will end up with following error :

Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://chromedriver.storage.googleapis.com/index.html
	at com.google.common.base.Preconditions.checkState(Preconditions.java:199)
	at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:109)
	at org.openqa.selenium.chrome.ChromeDriverService.access$0(ChromeDriverService.java:1)
	at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:137)
	at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:296)
	at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:88)
	at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:116)

Sample Script


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Test1 {

	public static void main(String[] s) throws InterruptedException {
		// Set chrome driver path
		System.setProperty("webdriver.chrome.driver", "D:/workspace/SampleProject/driver-servers/chromedriver.exe");

		// Initialize Chrome Driver
		WebDriver driver = new ChromeDriver();

		// Launch Facebook on Chrome Browser
		driver.get("http://www.facebook.com/");

		// Wait for 5 seconds
		Thread.sleep(5000);

		// Close Browser
		driver.close();

	}
}

If you really like the information provided above, please don’t forget to like us on Facebook, you can also leave the comment.

Leave a Reply

Your email address will not be published. Required fields are marked *