Javascript required
Skip to content Skip to sidebar Skip to footer

How to Open a Chrome Browser in Selenium

Introduction

Selenium automates a browser. There are multiple browsers supported by Selenium WebDriver. Major browsers are – Google Chrome, Mozilla Firefox, Microsoft Internet Explorer, Microsoft Edge, Opera, and Safari . In this post, we will learn to " Launch a chrome browser in Selenium WebDriver 3 and 4 ".

Install Chrome browser

First, check if you have a Chrome browser installed in your system. If it is installed then jump to the next step otherwise install the Chrome browser from here. Installation is just like any other software installation. Click on the installation file and Next Next Next……

Setup a Selenium WebDriver Java project

Since we will use Java binding of Selenium WebDriver to launch a Chrome browser, please set up a Selenium Java project as discussed here.

Use ChromeDriver class of Selenium WebDriver

Selenium WebDriver provides corresponding classes for browsers. For example:- For Chrome -> ChromeDriver, For Firefox -> FirefoxDriver . To launch any browser through Selenium WebDriver we just need to create an object of their corresponding class.

Selenium WebDriver Code

package BasicSeleniumConcepts ;

import org . openqa . selenium . chrome . ChromeDriver ;

public class LaunchChromeBrowser {

public static void main ( String [ ] args ) {

ChromeDriver firefoxDriver = new ChromeDriver ( ) ;

firefoxDriver . get ( "https://www.google.com/" ) ;

}

}

Output

Downloading chromedriver for chrome browser

We were expecting that a chrome browser should have launched but we got an exception IllegalStateException  and states that " The path to the driver executable must be set by the webdriver.chrome.driver system property ".

Selenium WebDriver can not communicate with a browser directly. We need an intimidatory executable agent between Selenium WebDriver and the actual browser. These intimidatory are provided by third parties. For chrome, we need a chromedriver as an interface between Selenium WebDriver and a real chrome browser.

There is a critical part that for each version of the Chrome browser, you may have different chromedriver executable version. Many times you may face issues when Selenium will not be able to create a WebDriver session and maximum time reason is an incompatibility of Chrome browser version and chromedriver version.

I will advise you to download the latest chrome browser and chromedriver always. If you are using an older version of Chrome browser due to some restrictions always readreleasenotes to know which chromedriver version will support your current version of chrome browser.

I have below version of chrome browser installed on my system.

Let's find a compatible version of chromedriver for my installed version of chrome. Remember this is just for reference. These versions will be changed with time. So just to follow the above process and download the correct version of chromedriver.

Just click on the link and select as per your operating system and click on that. It will start downloading. It is a zipped file that you need to unzip. After unzip you will get a chromedriver.exe file as exe is an extension of executable file on Windows. For Mac and Linux there will be no extension as they follow permission-based capability to identify an executable file.

For Windows, chromedriver_win32 works for both 32 and 64 bit systems.

Note:- You can rename this file whatever you want. There are no restrictions that you can not change its name. Many beginners get confuse on this. You just need to use the changed name in your program.

Before launching a Chrome browser you need to let WebDriver know explicitly the location of chromedriver executable file. As of now, we will add the path of chromedriver as a system property. There are some other ways as well which we will see later. Any System property is set as a key-value pair and to set path of chromedriver, selenium WebDriver has a key as " webdriver.chrome.driver ". Remember you can not change the key to some other name.

Remember we need to set a path of chromedriver with the extension of it if available. It is needed for Windows but for Linux and Mac, you need to give it executable permission.

Selenium WebDriver Code

package BasicSeleniumConcepts ;

import org . openqa . selenium . chrome . ChromeDriver ;

public class LaunchChromeBrowser {

public static void main ( String [ ] args ) {

System . setProperty ( "webdriver.chrome.driver" , "C:\\Users\\amomahaj\\Downloads\\chromedriver_win32 (2)\\chromedriver.exe" ) ;

ChromeDriver firefoxDriver = new ChromeDriver ( ) ;

firefoxDriver . get ( "https://www.google.com/" ) ;

}

}

Output

Now you will see a chrome browser will be launched and Google homepage will be loaded.

Do not worry about seeing these red lines. They are just logs. You can ignore it as of now. It has some important messages which will see later.

You can download/clone the above sample project from here.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning

Find all Selenium related posts here, all API manual and automation related posts here, and find frequently asked Java Programs here.

Many other topics you can navigate through the menu.

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

How to Open a Chrome Browser in Selenium

Source: http://makeseleniumeasy.com/2020/08/05/how-to-launch-chrome-browser-in-selenium-webdriver-java/