Execute Selenium Test on Chrome Browser
To execute Selenium script on Chrome Browser, first you have to download the ChromeDriver server.
Why ChromeDriver server is Required?
As ChromeDriver.exe is a separate executable file used by Selenium WebDriver to communicate with Chrome Browser.
Selenium WebDriver script interacts with ChromeDriver server through JSON wire Protocol and then ChromeDriver Server further communicate with Chrome Browser. So basically, ChromeDriver Server acts as a bridge between Selenium and Chrome Browser. Just have a look at below picture :
ChromeDriver is supported by the Google Chromium team and it can be downloaded from https://sites.google.com/a/chromium.org/chromedriver/
Using above download link, You would be able to download latest as well as older versions of ChromeDriver. Our recommendation is to download the latest stable release to avoid unwanted issues.
NOTE: For windows environment, you need to download the windows compatible version : chromedriver_win32.zip
Command to set the ChromeDriver path into selenium script
System.setProperty("webdriver.chrome.driver", "D:/Selenium/chromedriver.exe");
NOTE: If you try to execute your selenium script on Chrome Browser without setting up the path of ChromeDriver Server, 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:/Selenium/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.