How do I open Chrome in selenium WebDriver?

SeleniumAutomation TestingTesting Tools

We can open Chrome browser in Selenium webdriver. We can launch Chrome by instantiating an object of the ChromeDriver class with the help of the below statement.

WebDriver driver = new ChromeDriver();

Next we have to download the chromedriver and configure it to our project by following the below step by step processes −

  • As per version of the Chrome browser in the system, we have to select the download link. The next page shall be navigated where links to chrome drivers compatible with various operating systems are present.

  • After selecting the chrome driver as per the system configuration for download, a zip file gets created. We need to extract that and save the chromedriver.exe file at any location.

Let us discuss how to configure chromedriver with System properties within the Selenium code −

  • Add the System.setProperty method in the code which takes the browser type and the path of the chromedriver executable path as parameters.

System.setProperty("webdriver.chrome.driver","<chromedriver path>");

Example

Code Implementation

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class ChromeBrw{
   public static void main(String[] args) {
      // creating object of ChromeDriver
      WebDriver driver = new ChromeDriver();
      // to configure the path of the chromedriver.exe
      System.setProperty("webdriver.chrome.driver", "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe");
      String url = "https://www.tutorialspoint.com/index.htm";
      driver.get(url);
   }
}
raja
Published on 26-Oct-2020 05:58:56
Advertisements