Alerts and Popups in Selenium

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
  • Pega testing training
    PEGA Testing Training
  • Today application testing is the deciding factor to launch the application into the market. And people do not launch the application unless it goes true.

  • blockchain course
    Blockchain Online Course
  • Today many people were enthusiastic, to know the exact details of things happening around him. This can get the proper knowledge on Blockchain.

  • ethical hacking course
    Ethical Hacking Certification Course
  • Zeal to learn ethical hacking is common among college students and IT professionals. Because everybody wants to secure their system from cyber attacks.

  • python training
    Python Online Training
  • Python is a dynamic interrupted language which is used in wide varieties of applications. It is very interactive object oriented and high-level programming language.

  • Tableau Training
    Tableau Training
  • Tableau is a Software company that caters interactive data visualization products that provide Business Intelligence services. The company’s Head Quarters is in Seattle, USA.

  • what is pega
    PEGA Online Training
  • Pega Systems Inc. is a Cambridge, Massachusetts based Software Company. It is known for developing software for Customer Relationship Management (CRM) and Business process Management (BPM).

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