Pop up and alerts in Selenium Webdriver

Posted By : Jatin Singh | 29-Mar-2020

Pop up and alerts in Selenium Webdriver


Testing a Webpage needs a certain set of rules. A simple example would be filling out a form on a webpage. We may have missed out certain fields/check box, and the system will throw some warnings in the form of Alerts. These alerts are also known as "Pop-ups".

Let’s delve into how to handle alerts and pop-ups by answering the following questions:

  • What is an Alert?
  • Why is an Alert used in Selenium?
  • Types of Alerts in Selenium and How to handle them?
  • Application areas of Alerts


What is an Alert?


:-> An  Alert is a message/notification box that notifies the user that they have missed out on some information that needs to be filled. It informs the user that some information needs their attention.


Why is an Alert used in Selenium?


:->Alert is basically used to display a warning message. It is a pop-up window that comes up on the screen. There are many user activities that result in an alert. For eg., if you click on a button that shows a message or maybe when you enter a web form, the HTML page asks you for some extra information. It is called an Alert.


Types of Alerts in Selenium:


There are mainly 3 types of Alerts, namely:

  1. Simple Alert
  2. Prompt Alert
  3. Confirmation Alert


Simple Alert: 

A simple alert just contains a "OK" button. They are used to show information to the user.


The syntax below will read the simple alert:

Alert simpleAlert = driver.switchTo().alert();


Prompt Alert:

In Prompt alert, the user needs to give some input to the field where a prompt alert is displayed. Prompt Alert is displayed when some user input is required.

We can use send keys to send the data to the respective field.

Syntax : promptAlert.sendkeys("Your Text");


Confirmation Alert:

Confirmation alert is used to accept or dismiss the alert.

Syntax : confirmationAlert.dismiss();


How to handle Pop-ups in Selenium Webdriver:

In Selenium there are multiple ways to handle the Pop-ups, the syntax for handling the pop-up are given below:


1- Driver.getWindowHandles();

In order to handle the opened windows by Selenium webdriver, we can use Driver.getWindowHandles() to switch between different windows.


2- Driver.getWindowHandle();

When the webpage is successfully loaded, we can handle the window by using driver.getWindowHandle(). It will handle the current window that identifies within the driver instance.


Alerts in Selenium: Application areas

  • Alert functions are used mainly in banking, e-commerce sites. 
  • Online application forms.

Selenium Webdriver is a browser automation framework that executes cross-browser tests. It supports Java, C#, PHP, HTML, Python, Perl, and Ruby. We are an ERP Development company with expertise in developing customized software solutions for our clients. Our developers use a vast technology stack to furnish clients with the best tools for their ERP. Our custom development services enable businesses to automate, plan, collaborate, and execute their operations methodically. 



Please remove URL from text

Comment is required

Sending message..
Connect with us