How To Handle Javascript Alerts/PopUps In Selenium

How To Handle Javascript Alerts/PopUps In Selenium


Alerts and popups are one of the important features in any modern-day web application and handling it via Selenium sometimes could be tricky.

But before moving further let's understand about different types of popups in any web application.




In SELENIUM, pop up is categorized into the following types. 

1. Javascript Popup 
2. Hidden Division Popup 
3. File Upload popup 
4. File download popup 
5. Child browser popup 
6. Window popup 

In this article, we will be looking more into javascript popup which further has been broadly divided into Alert, confirmation and prompt popup.

Javascript Pop up: 

This pop up is subdivided into below mentioned 3 pop-ups. 

1. Alert pop up 
2. Confirmation pop up 
3. Prompt pop up 

To understand how to handle Notification popup in Selenium. Refer to this link.

1. Alert Pop up: 


The main Characteristics features of this pop-up are:

● We can’t inspect this pop-up and move this kind of popup.
● This pop up will have a white color background with the black color font. 
● This pop up will have only one “OK” button 

Steps to handle Alert pop up 

To handle the alert pop up, we first have to switch to an alert window using the below statement. 

driver.switchTo().alert(); 

After transferring the control to the alert window, we can use the following methods of the “Alert” interface. 

getText() to get the text present on the alert window. 

accept() / dismiss() to click on OK button on the alert window. 

Sample code to handle Alert Popup




2. Confirmation Pop up



Characteristics features of this pop-up are : 

● We can’t inspect this pop-up. 
● We can’t move this kind pop up. 
● This pop up will have a white color background with the black color font. 
● This pop up will have two buttons:- “OK” button and the “Cancel” button. 



Steps to handle Confirmation pop up 

To handle the alert pop up, we first have to switch to an alert window using the below statement. 

driver.switchTo().alert(); 

After transferring the control to the alert window, we can use the following methods of the “Alert” interface. 

Alert.accept()--To Accept the alert.

Alert.dismiss()--To Dismiss the alert

Sample code to handle confirmation Popup




3. Prompt Alert Pop-up



A prompt box is often used if we want the user to input a value before entering a page.

When a prompt box pops up, the user will have to click either "OK" or "Cancel" to proceed after entering an input value.

If the user clicks "OK," the box returns the input value. If the user clicks "Cancel" the box returns null.



Steps to handle Prompt Alert pop up 

● To handle the alert pop up, we first have to switch to an alert window using the below statement. 

driver.switchTo().alert(); 

● After transferring the control to the alert window, we can use the following methods of the “Alert” interface. 

getText() to get the text present on the alert window. 
sendKeys() to enter a text in the textbox on the alert window
accept() to click on the “OK” button on the alert window.
dismiss() to click on the Cancel button on the alert window.

Sample code to Handle Prompt Alert Popup



Different types of Alert popup exceptions that we usually face

1. NoAlertPresentException: When the alert pops up is physically not available on the page, and we still try to either switch to the alert or try to perform any action on the alert popup, we get this exception.

2. UnhandledAlertException: We get this exception when we perform any action on the webpage without handling the alert popup.

To know more about different types of exceptions and how to handle them in selenium. Refer to this article.




SHARE THIS

Author:

My Name is Ankur Jain and I am currently working as Automation Test Architect.I am ISTQB Certified Test Manager,Certified UI Path RPA Developer as well as Certified Scrum Master with total 12 years of working experience with lot of big banking clients around the globe.I love to Design Automation Testing Frameworks with Selenium,Appium,Protractor,Cucumber,Rest-Assured, Katalon Studio and currently exploring lot in Dev-OPS as well. I am currently staying in Mumbai, Maharashtra. Please Connect with me through Contact Us page of this website.

Previous Post
Next Post