i

Selenium Step By Step Guide

Working with Chrome Driver - Part 1

Selenium scripts run in different browsers, and Chrome is one the most used browser based on the compatibility of many web applications. We will see how to configure the chrome driver to run the Selenium code into the Chrome browser.

Let us discuss the steps to follow for configuring the Chrome driver into our Selenium code:

  1. Set the property for the driver path.
  2. Setting up Chrome options using ChromeOptions class.
  3. Instantiate the Chrome driver object.
  4. Launch the URL using the driver object.

Set the property for the driver path:

String driverPath="C:\\Users \\chromedriver.exe";

System.setProperty("webdriver.chrome.driver",driverPath);

Alternatively, we can set the driver path under “Environment Variables”, this way we can skip setting path from the code.

Select the Path variable section under System Variables and click on the Edit button. Now put a semicolon “;” at the end of the path variable’s string and enter the full path of the directory having Chrome Driver executable file.

 

Setting up ChromeOptions, DesiredCapabilities and Instantiating the ChromeDriver:

        ChromeOptions options=new ChromeOptions();

        options.addArguments("start-maximized");

        DesiredCapabilities dc=new DesiredCapabilities();

        dc.setCapability(ChromeOptions.CAPABILITY, options);

        WebDriver driver=new ChromeDriver(dc);

Launching the URL in chrome browser:

Now we have instantiated the ChromeDriver object, and its time to launch the URL in chrome browser.

        driver.get("https://selflearning.io/");

Let us see the complete sample code to set up the chrome driver for running a Selenium script.

package SeleniumTest.SeleniumTest;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.chrome.ChromeOptions;

import org.openqa.selenium.remote.DesiredCapabilities;

public class ChromeDriverTest

{

    public static void main( String[] args )

    {

       String driverPath="C:\\Users \\chromedriver.exe";

        System.setProperty("webdriver.chrome.driver",driverPath);

        ChromeOptions options=new ChromeOptions();

        options.addArguments("start-maximized");

        DesiredCapabilities dc=new DesiredCapabilities();

        dc.setCapability(ChromeOptions.CAPABILITY, options);

        WebDriver driver=new ChromeDriver(dc);

        driver.get("https://selflearning.io/");

    } 

}