Alerts and Popups in Selenium

Click to rate this post!
[Total: 0 Average: 0]

The next topic that we need to discuss in the selenium testing tutorial is Alert handling. And now we will initially start with

what is an Alert()?

An alert is a small message box that displays an on-screen notification. This gives the user some kind of information (or) ask permission for certain kind of options. An alert usually gives a warning to the user.

Alerts are classified into the following types:

Simple Alert():

A simple Alert displays some information (or) warning on the screen.

alerts and popups in selenium

Prompt alert():

A prompt alert usually asks some input from the user and selenium web driver can enter the text using send keys (“input”)

 alerts and popups in selenium 

Confirmation alert():

A confirmation alert asks some input from the user to do some kind of operation

alerts and popups in selenium

In learn selenium online we will discuss

How to handle alerts in Selenium Webdriver?

Selenium web driver provides an alert () interface that contains the following methods

Method name Function Description
Void dismiss() driver.switchTo().alert().dismiss(); This is used to cancel the alert button
Void accept() driver.switchTo().alert().alert(); This is used to click OK button of the alert()
StringgetText() driver.switchTo().alert().getText(); It is used to capture the alert message
Void sendKeys() driver.switchTo().alert().sendkeys(“text”); It is used to send some data to the alert box

We can implement these methods in Selenium web driver using the following code

import org.openqa.selenium.By;

//import web drivers
import org.openqa.selenium.WebDriver;

// import exception classes
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.Alert;

public class AlertDemo {

public static void main(String[] args) throws NoAlertPresentException,InterruptedException {
WebDriver driver = new ChromeDriver();

// Alert Message handling



// Switching to Alert
Alert alert = driver.switchTo().alert();

// Capturing alert message.
String alertMessage= driver.switchTo().alert().getText();

// Displaying alert message

// Accepting alert


And the next topic in Selenium training is Pop- up

what is a pop- up?

It is an ability to interact with multiple windows including alerts.  This allows switching control to pop-up while keeping the browser in the background. And soon after the operation completion, this would return to the main window.

How to handle Selenium pop-ups using Selenium Web driver?

it training says the following methods can handle multiple windows


This method is used to handle all the web driver, browser windows. In a web application, this allows you to switch from one window to the other. It usually returns the string Iterator.

Driver. getWindowHandle():

When the site is opened this method is used to handle the main window. This will usually handle the current window that uniquely identifies within the driver instance.

And by executing the following code, we handle the Selenium popp-ups:

import java.util.Iterator;
import java.util.Set;
//import Selenium packages
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
// import borwser drivers
import org.openqa.selenium.firefox.FirefoxDriver;

public class WindowHandle_Demo {

public static void main(String[] args) throws InterruptedException {
WebDriver driver=new FirefoxDriver();

//Launching the site.


String MainWindow=driver.getWindowHandle();

// To handle all new opened window.
Set<String> s1=driver.getWindowHandles();
Iterator<String> i1=s1.iterator();



// Switching to Child window


// Closing the Child Window.
// Switching to Parent window i.e Main Window.

Example 2 :


In real time projects, there are multiple windows. And these multiple windows throws multiple pop-ups. And have you think about how to handle all these pop-ups? Strictly speaking, have you think about how to handle these multiple windows. If so, I come with a solution for you. \


selenium Web driver has the capability to handle multiple windows using the Switch Case. Execute the following code in your Eclipse IDE to handle multiple pop-ups

package softwareTestingMaterial;
import org.testng.annotations.Test;

//import JAVA packages
import java.util.Iterator;
import java.util.Set;

//import web drivers

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class MultipleWindowsClass{
public void testMultipleWindows() throws InterruptedException{
System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir")+"\src\test\java\drivers\geckodriver.exe");
// To open browser
WebDriver driver=new FirefoxDriver();
// To maximize browser
// To open slenium tutorial with multiple windows
// It will return the parent window name as a String
String mainWindow=driver.getWindowHandle();
// It returns no. of windows opened by WebDriver and will return Set of Strings
Set<String> set =driver.getWindowHandles();
// Using Iterator to iterate with in windows
Iterator<String> itr= set.iterator();
// Compare whether the main windows is not equal to child window. If not equal, we will close.
// This is to switch to the main window

While  training at online educationmany people asked me what is the difference between  quit() and close()?

driver.quit()  is used to quit the whole browser session along with all the associated browser windows , tabs ad pop-ups.
driver.close() is used to close the current browser window. if there is only one browser open, it quick to whole browser session.

to our newsletter

Drop Us A Query

Trending Courses
  • oracle 12c rac | OnlineITGuru
    Oracle RAC Training
  • Oracle is the large vendor in providing the various storge services to the people across the globe. This vendor provides a different amount of storage services to the people across the globe.

  • salesforce lightning training | OnlineITGuru
    Salesforce Lightning Training
  • Developing an application is not a simple and easy task. There are various parameters that the web developer need to take care while developing an application. One of those parameters that the developer needs to take care of is the code reusability.

  • Selenium with python
    Selenium with Python Training
  • As we know, that Selenium with Python Web Browser Selenium Automation is Gaining Popularity Day by Day. So many Frameworks and Tools Have arisen to get Services to Developers.

  • machine learning with python
    Machine Learning with Python Training
  • Over last few years, Big Data and analysis have come up, with Exponential and modified Direction of Business. That operate Python, emerged with a fast and strong Contender for going with Predictive Analysis.

  • Data science with R
    Data Science With R Training
  • Understanding and using Linear, non-linear regression Models and Classifying techniques for stats analysis. Hypothesis testing sample methods, to get business decisions.

  • data science with python
    Data Science with Python Training
  • Everyone starts Somewhere, first you learn basics of Every Scripting concept. Here you need complete Introduction to Data Science python libraries Concepts.

100% Secure Payments. All major credit & debit cards accepted.

Call Now Button