Skip to main content

How to Launch Chrome Browser in Selenium

To launch a chrome browser using selenium. First we need to download the chrome driver. 

Step1: Go to Help->About Google Chrome to check your browser version.

launch chrome browser in selenium

Step 2: Open this link or you can search in google by typing "download chromedriver"

Step 3: Click on the latest stable release if you have updated your browser to latest version otherwise click on the link which match your browser version.

selenium code to open chrome browser

Step 4: Here driver file is different for each operating system Windows exe will not work on Mac or Linux system. Once you will click on link, download will start.

how to launch a chrome browser in selenium

Step 5: Unzip and Paste downloaded file to drivers folder or directory of your choice.

Step 6: Now open your driver configuration file or class file and paste below code. Since we have not introduced TestNG or JUnit yet, we will be executing this with Java runner using main class.

Launch Chrome Browser in Selenium Java

package com.pb.seltest.util;

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

public class AppTest 
{
	public static void main(String args[])
	{
		System.setProperty("webdriver.chrome.driver",System.getProperty("user.dir") + "//drivers//chromedriver");
		
		WebDriver driver;
		driver = new ChromeDriver();
		driver.get("http://www.automationpractice.com");
	}
}

In above code,

  • setProperty basically sets the system property to a value named webdriver.chrome.driver, and the path is mentioned to get the chrome driver. Here, usr.dir will get the current user working directory, and then we have added the chrome driver path.
  • WebDriver is the main interface for testing, representing an idealized web browser. The methods in the WebDriver interface fall into three categories: Control of the browser itself, Selection of WebElements, and Debugging aids.
  • new ChromeDriver(), Here we are creating an instance of ChromeDriver; Chrome Driver is a class that extends RemoteWebDriver class.
  • get() will load a new web page in the current browser window. This is done using an HTTP GET operation, and the method will block until the load is complete.

Video Tutorial: How to Launch a Chrome Browser in Selenium

Submitted by tgoswami on September 23, 2020

Tarun has 11+ years of experience in Quality Assurance in different domains like Banking, E-commerce, Health and Education.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.