ChromeDriver Selenium Tutorial: Selenium Webdriver Tests on Chrome

By Sruthy

By Sruthy

Sruthy, with her 10+ years of experience, is a dynamic professional who seamlessly blends her creative soul with technical prowess. With a Technical Degree in Graphics Design and Communications and a Bachelor’s Degree in Electronics and Communication, she brings a unique combination of artistic flair…

Learn about our editorial policies.
Updated March 7, 2024

In-Depth Tutorial On ChromeDriver for Running Selenium Webdriver Tests on Chrome Browser:

Handling browser alerts while automating through Selenium will be discussed in this article.

Moreover, we will elaborate on the set up of the Selenium script for the Google Chrome browser along with appropriate examples and pseudo-codes.

Upon going through this article, you will also be able to set up Chrome for Selenium and will be in a position to handle browser-specific alerts.

Selenium on Chrome

How To Download ChromeDriver For Selenium?

We assume that you have already installed the Google Chrome browser. The next step is to find the appropriate version of the ChromeDriver. Chromedriver is a .exe file that your WebDriver interface uses to initiate the Google Chrome browser.

As this is an open tool, you can download it from its official website or the Selenium community. The only point that you need to consider is that the version of your Chrome browser should be compatible with the chromedriver.exe that you are going to download.

Below are the steps to follow while configuring the chrome setup for Selenium.

#1) Check the version of the chrome.

Open Chrome Browser -> Help -> About Google Chrome

Open Chrome Browser

Check the Chrome Version

#2) Open Chromedriver.exe downloads where you will see the latest ChromeDriver for the latest google chrome version. We will download version – 75 of chromedriver.exe

Chromedriver.exe downloads

#3) Download the chromedriver.exe file for the respective OS and copy that .exe file into your local.

Copied the downloaded .exe

#4) The path of the chromedriver (C:\webdriver\chromedriver.exe) will be used in our program.

Selenium Setup With ChromeDriver

Now that we are done with setting up of ChromeDriver, we will launch the Eclipse software for executing our Selenium codes.

Below are the steps to follow to create and execute our Selenium codes on Eclipse.

Create A New Maven Project

This step will let you create an empty Maven project in which you can execute your Selenium codes.

All you need to do is to click on File -> New -> Others -> Maven Project.

Click on File and then New

Click on Maven Project

create a simple maven project

Enter the group id and artifact id

Add Dependencies

In the above diagram, we have added the group id and artifact id. The same will be reflected or required in your pom.xml after you have clicked on the finish button.

pom.xml

Pom.xml is a file that contains the dependencies. Here we can add as many dependencies as we like. The dependencies could be Selenium, GitHub, TestNG and so on.

pom.xml file with dependencies

Project BuildPath And Importing Jars

The next step is to download the jar files and import them in your project. You can download all the selenium jars from the google or the official maven site

After you have downloaded all the jars, you need to follow the below steps in order.

  • Right-click on your Maven Project and click on Properties.

Select Properties

  • Click on Java Build Path -> Libraries -> Add Jars -> Apply and Close.

Adding Jars in Java Build Path

Handling Chrome Alerts

We have set up our Maven. Now we will proceed with handling browser alerts through automation.

You may think what are Browser Alerts? Browser Alerts are those alerts which are browser-specific and the same alert may or may not pop up when you are using a different browser.

Example: Let’s take the example of Facebook. Whenever you try automating www.facebook.com using Chrome, you will see the following alert.

Script for Facebook login

In the above script, we have passed our ChromeDriver path as an argument in the system.setProperty(). This will let the WebDriver to control Google Chrome.

Upon executing the above script, we will be logged into Facebook using the email id and password. However, an alert will pop up which would further deny any operation that we will do on the website through our script.

Below is the image of how the pop up will look like.

Facebook Login Browser Alert

The same type of alert can be seen on Myntra, Flipkart, Makemytrip, Bookmyshow, etc. These are browser-specific alerts which can be handled using the ChromeOptions class.

ChromeOptions Class

ChromeOptions class is a class for ChromeDriver which has methods for enabling various ChromeDriver capabilities. One such capability is to disable the notifications that we get while logging into some of the commercial websites.

Below are the pseudo-codes for handling such alerts.

#1) For Google Chrome With Version <= 50

ChromeOptions options = new ChromeOptions();
options.addArguments(“--disable--notifications”);

Code for Handling Notifications and Alerts at Browser Level <=50

Complete Code to Practice:

package tests;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class HandlingAlerts {
	
             public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.setProperty("webdriver.chrome.driver", "C:\\webdriver\\chromedriver.exe");
		ChromeOptions options = new ChromeOptions();
		options.addArguments("--diable--notifications");
		WebDriver driver = new ChromeDriver(options);
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
		driver.get("https://www.facebook.com");
		WebElement element = driver.findElement(By.xpath("//*[@id = 'email']"));
		element.sendKeys("email id");
		WebElement element2 = driver.findElement(By.xpath("//*[@id = 'pass']"));
		element2.sendKeys("password");
		
		element2.submit();
	}
}

#2) For Google Chrome With Version > 50

HashMap<String, Object> map = new HashMap<String, Object>();
map.put("profile.default_content_setting_values.notifications", 2);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", map);
WebDriver driver = new ChromeDriver(options);

Complete code for Handling Browser (version is more than 50) alerts and notifications

Complete Code to Practice:

package tests;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class HandlingAlerts {
	
          public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.setProperty("webdriver.chrome.driver", "C:\\webdriver\\chromedriver.exe");
		HashMap&amp;lt;String, Object&amp;gt; map = new HashMap&amp;lt;String, Object&amp;gt;();
		map.put("profile.default_content_setting_values.notifications", 2);
		ChromeOptions options = new ChromeOptions();
		options.setExperimentalOption("prefs", map);
		WebDriver driver = new ChromeDriver(options);
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
		driver.get("https://www.facebook.com");
		WebElement element = driver.findElement(By.xpath("//*[@id = 'email']"));
		element.sendKeys("email id");
		WebElement element2 = driver.findElement(By.xpath("//*[@id = 'pass']"));
		element2.sendKeys("password");
		
		element2.submit();
	}
}

Explanation of Both the Code Snippets:

The first code is for all the Chrome browsers with versions less than 50. It is a very simple code where we have created an instance of the class called ChromeOptions and passed it on in the ChromeDriver.

The second code has made use of the collection class. As we all know Java Collections, we have used HashMap with keys and values as String and Object. Then we have used the put() function for overriding the default setting of the browser.

Lastly, we have used setExperimentalOption() method to set our preferences for the browser.

Output of the Script

Conclusion

Upon going through the above concepts like how to create and set up a maven project from the scratch, adding dependencies in your pom.xml and configuring the build path, you will be able to create your maven project.

Moreover, we have elaborated on the concepts related to ChromeDriver and Chromeoptions class which would help you to configure your Selenium with Google Chrome Browser with ease and let you handle any type of alerts, notifications, and pop-ups on the Chrome browser.

We hope you enjoyed reading this ChromDriver Selenium tutorial!!

Was this helpful?

Thanks for your feedback!

Leave a Comment