You are on page 1of 14

12/13/2018 Alert & Popup Window Handling in Selenium WebDriver


Home (/) Testing

SAP Web Must Learn! Big Data

Live Projects AI Blog (/blog/)

Alert & Popup Window Handling in Selenium

In this tutorial, we will learn about different types of alert found in web application Testing
(/software-testing.html)and how to handle Alert in Selenium WebDriver. We will also see how
do we accept and reject the alert depending upon the alert types.

In this tutorial, you will learn-

What is Alert?
How to handle Alert in Selenium WebDriver
How to handle Selenium Popup window using Webdriver

What is Alert?
Alert is a small message box which displays on-screen notification to give the user some
kind of information or ask for permission to perform certain kind of operation. It may be also
used for warning purpose.

Here are few alert types:

1) Simple Alert

This simple alert displays some information or warning on the screen.


2) Prompt Alert. 1/14
This Prompt
12/13/2018 Alert asks some input Alert
from the user
& Popup Windowand selenium
Handling in Seleniumwebdriver
WebDriver can enter the text
using sendkeys(" input…. ").


3) Confirmation Alert.

This confirmation alert asks permission to do some type of operation.


How to handle Alert in Selenium WebDriver

Alert interface provides the below few methods which are widely used in Selenium
Webdriver. 2/14
1) void
12/13/2018 dismiss() // To click on the Alert
'Cancel' button
& Popup Window of the
Handling alert.WebDriver
in Selenium


2) void accept() // To click on the 'OK' button of the alert.


3) String getText() // To capture the alert message.


4) void sendKeys(String stringToSend) // To send some data to alert box.


You can see a number of Alert methods are displayed as shown in below screen suggested
by Eclipse.

We can easily switch to alert from the main window by using Selenium's .switchTo()


Now we automate the given below scenario.

In this scenario, we will use Guru99 demo site to illustrate Selenium Alert handling. 3/14
12/13/20181) Launch the web browser and
& Popupthe siteHandling in Selenium WebDriver

( "

Step 2) Enter Any Customer id.



Step 3) After entering the customer ID, Click on the "Submit" button.



Step 4) Reject/accept the alert. 4/14
12/13/2018 Alert & Popup Window Handling in Selenium WebDriver


Handling Alert in Selenium Webdriver using above scenario 5/14
12/13/2018 Alert & Popup Window Handling in Selenium WebDriver
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.Alert;

public class AlertDemo {

public static void main(String[] args) throws NoAlertPresentException,InterruptedEx

ception {
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

Output :

When you execute the above code, it launches the site. Try to delete Customer ID by
handling confirmation alert that displays on the screen, and thereby deleting customer id
from the application.

How to handle Selenium Pop-up window using Webdriver 6/14
In automation,
12/13/2018 when we have multiple windows
Alert & in Handling
Popup Window any web application,
in Selenium WebDriver the activity may need

to switch control among several windows from one to other in order to complete the
operation. After completion of the operation, it has to return to the main window i.e. parent
window. We will see this further in the article with an example.

In selenium web driver there are methods through which we can handle multiple windows.


To handle all opened windows by web driver, we can use "Driver.getWindowHandles()" and
then we can switch window from one window to another in a web application. Its return type
is Iterator<String>.


When the site opens, we need to handle the main window by driver.getWindowHandle().
This will handle the current window that uniquely identifies it within this driver instance. Its
return type is String.

To handle multiple windows in Selenium WebDriver, We follow the following steps.

Now, we will automate the given below scenario to see how to handle multiple windows
using Selenium Webdriver.

In this scenario, we will use "Guru99" demo site to illustrate window handling.

Step 1) Launch the site.

Launch the browser and open the site " "



Step 2) Click on link "Click Here ".

When the user clicks on the " Click Here " link, new child window opens.

(/images/3-2016/032216_1314_AlertPopuph12.png) 7/14
12/13/2018 3) New Child Window opens. Alert & Popup Window Handling in Selenium WebDriver

A new window opens, ask the user to enter email id and submit the page.


Step 4) Enter your email ID and submit.



Step 5) Display the Access Credentials on submitting the page.


2016/032216_1314_AlertPopuph15.png) 8/14
12/13/2018 you execute the code, you will
Alertsee theWindow
& Popup childHandling
window is open
in Selenium in new tab.

1. Close the Child window on which credentials are displayed.



2. Switch to the parent window.


Handling multiple windows in selenium webdriver using above scenario. 9/14
12/13/2018 Alert & Popup Window Handling in Selenium WebDriver
import java.util.Iterator;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
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


.sendKeys(" (");


// Closing the Child Window.

// Switching to Parent window i.e Main Window.

Output: 10/14
12/13/2018you execute the above code,Alert
it launches theHandling
& Popup Window site and on clicking
in Selenium WebDriver the link "Click here,"

it opens up a child window in a new tab. You can close the child window, and switch to the
parent window once the operation is completely done. Hence handling more than one
window in the application.



We defined the types of alert and shown them with a screen shot.
Demonstrated handling the Alert with Selenium WebDriver using particular scenario.
Handled multiple windows with Selenium WebDriver using particular scenario.

 Prev (/handling-date-time-picker-using-selenium.html) Report a Bug

Next  (/handling-dynamic-selenium-webdriver.html)



(/xslt-report- (/find-broken-links- (/selenium-webtable.html)

selenium.html) selenium-webdriver.html) (/selenium-
(/xslt-report- (/find-broken-links- webtable.html)
selenium.html) selenium-
How to Handle Web Table in
XSLT Report in Selenium webdriver.html) Selenium WebDriver 11/14
12/13/2018 How to& Find
Alert All/Broken
Popup Window Handling links
in Selenium(/selenium-webtable.html)

using Selenium Webdriver



(/handling-ajax-call- (/breakpoints-startpoints- (/selenium-csharp-

selenium-webdriver.html) selenium.html) tutorial.html)
(/handling-ajax-call- (/breakpoints- (/selenium-csharp-
selenium- startpoints- tutorial.html)
webdriver.html) selenium.html) Selenium C# Webdriver
Handling AJAX Call in Breakpoint & Start Point in Tutorial for Beginners
Selenium Webdriver Selenium IDE (/selenium-csharp-
(/handling-ajax-call- (/breakpoints-startpoints- tutorial.html)
selenium-webdriver.html) selenium.html)

Selenium Tutorials
1) Introduction (/introduction-to-selenium.html)

2) Intro WebDriver (/introduction-webdriver-comparison-selenium-rc.html)

3) Install Webdriver (/installing-selenium-webdriver.html)

4) First WebDriver Script (/first-webdriver-script.html)

5) Locators (/locators-in-selenium-ide.html)

6) Find Element Selenium (/find-element-selenium.html)

7) Forms & Webdriver (/accessing-forms-in-webdriver.html)

8) CheckBox & Radio Button WebDriver (/checkbox-and-radio-button-webdriver.html)

9) Click Image Webdriver (/click-on-image-in-selenium.html)

10) Selenium Webdriver DropDown (/select-option-dropdown-selenium-webdriver.html)

11) Links & Tables (/accessing-links-tables-selenium-webdriver.html)

12) Keyboard Mouse Events (/keyboard-mouse-events-files-webdriver.html)

13) Upload & Download File (/upload-download-file-selenium-webdriver.html)

14) XPath in Selenium (/xpath-selenium.html)

15) Alert & Popup handling (/alert-popup-handling-selenium.html)

16) Handle Web Table (/selenium-webtable.html)

17) Handling Dynamic Web Tables (/handling-dynamic-selenium-webdriver.html)

18) Desired Capabilities in Selenium (/desired-capabilities-selenium.html) 12/14
12/13/2018 Alert & Popup Window Handling in Selenium WebDriver
19) Verify Tooltip WebDriver (/verify-tooltip-selenium-webdriver.html)

20) Find Broken links (/find-broken-links-selenium-webdriver.html)

21) Gecko (Marionette) Driver (/gecko-marionette-driver-selenium.html)

22) Install TestNG in Eclipse (/install-testng-in-eclipse.html)

23) Selenium & TestNG (/all-about-testng-and-selenium.html)

24) Introduction to TestNG Groups (/introduction-testng-groups.html)

25) Test Case Priority in TestNG (/test-case-priority-testng.html)

2) Install IDE & FireBug (/install-selenuim-ide.html)

3) Introduction IDE (/introduction-selenuim-ide.html)

4) First Script (/first-selenium-test-script.html)

6) Enhancements (/enhancing-selenium-ide-script.html)

7) Variables, Echo, Alert, PopUp (/store-variables-handling-selenium-ide.html)

 ( 
( 


About US (/about-us.html)
Advertise with Us (/advertise-us.html)
Write For Us (/become-an-instructor.html)
Contact US (/contact-us.html)

Career Sugges on
SAP Career Suggestion Tool (/best-sap-module.html)
Software Testing as a Career (/software-testing-career-
Certificates (/certificate-it-professional.html)

Interes ng
Books to Read! (/books.html)
Suggest a Tutorial
Blog (/blog/) 13/14
12/13/2018 Quiz (/tests.html)Alert & Popup Window Handling in Selenium WebDriver
Review (/best-ergonomic-mouse.html)

Execute online
Execute Java Online (/try-java-editor.html)
Execute Javascript (/execute-javascript-online.html)
Execute HTML (/execute-html-online.html)
Execute Python (/execute-python-online.html)

© Copyright - Guru99 2018

Privacy Policy (/privacy-policy.html) 14/14