All IT Courses 50% Off
Selenium Tutorials

PopUps and Alerts in Selenium 

Alerts in Selenium Frequently, when we complete a form and click the submit button, an alert stating “Contact Number is Required” appears. Sometimes we forget to check the terms and conditions box, and we receive a pop-up alert reminding us to do so. The alert box’s OK button returns me to the form, which won’t submit until we’ve completed all of the “required” fields. These usage cases demonstrate how important “Alerts” and “Popups” are in web applications. Check out the Selenium online training to learn more about Selenium Alerts and PopUps.

What do Selenium Alerts and Popups mean?

Alerts are tiny pop-up windows or boxes that show messages or notifications and ask the user for permission to perform specific tasks in exchange for some information. Furthermore, we can make use of them for warning purposes. Occasionally, the user can enter a few details in the alert box as well.

Application alerts force the user to read the message provided and take appropriate action by shifting their focus from their existing browser window to a newly launched one. Users who ignore the alert message box will be barred and unable to continue working.

What are the different types of alerts/popups?

Selenium WebDriver may run across alarms while automating any online application. These alerts may depend on the program being automated or the operating system the user is using. These classifications allow us to classify the alerts mostly into the following groups:

All IT Courses 50% Off
  • Windows/OS Alerts: Window-based popups and alerts are produced by the system. These alerts and dialog boxes are displayed by the developers using the operating system APIs. Because Selenium is primarily a tool for automating testing web applications, handling these warnings in Selenium is a little complex and outside the scope of the WebDriver. Instead, we require a third-party utility to automate window-based popups.  Among these tools are Java’s Robot Class and AutoIT.
  • Web/Javascript/Browser-based Alerts: Web/Browser-based alerts, also known as Javascript alerts, are those that depend on the user’s browser. Most people refer to these warnings as popups.

What are the various kinds of alerts provided by Web Applications?

As we previously covered, web application developers can incorporate a variety of warning types. To manage each of these alerts/popups, several actions are required. Here are some examples of the various warnings that the online applications offer:

  • Simple alert: There is an OK option on these warnings, which are merely informative. After viewing the message in the alert window, users can click the OK button.
  • Prompt Alert: The user is required to submit text in the alert box as part of the prompt alert’s input requirements. The user is presented with a prompt alert box where he or she can input their username and press the OK button or dismiss the alert box without entering any details.
  • Confirmation Alert: When the user accepts or rejects the message box, the alerts receive some user confirmation. They differ from prompt alerts in that there is no text field present, preventing the user from entering any information. The message may only be read and inputted by the user by clicking the OK/Cancel button.
PopUps and Alerts in Selenium 

How should I use Selenium WebDriver to handle popups and alerts?

As we are all aware, Selenium WebDriver always keeps the focus on the main browser window and will only run commands on the main browser window when any automation scripts are being run. But each time an alert or pop-up occurs, a new window is created. In order to handle the Alerts using Selenium WebDriver, the emphasis must be changed to the child windows opened by the Alerts. To switch the control from the parent window to the Alert window, the Selenium WebDriver  provides the following command:

driver.switchTo( ).alert( );

The methods supplied by the Alert Interface can be used to carry out the different necessary activities once the control has been transferred from the main browser window to the alert window. Examples include acknowledging the alarm, ignoring the alert, reading the text from the alert window, adding text to the alert window, and more.

Void accept(): This method clicks on the ‘OK’ button of the alert box.

driver.switchTo( ).alert( ).accept();

Void dismiss(): We use this method when the ‘Cancel’ button clicks in the alert box.

driver.switchTo( ).alert( ).dismiss();

String getText(): This method captures the message from the alert box.


Void sendKeys(String stringToSend): This method sends data to the alert box.


How to handle unexpected Alerts using Selenium WebDriver?

Unexpected alerts can occasionally appear while using a web application owing to an error or for other reasons. This kind of alert only appears occasionally; it does not appear each time you view the website. If an unexpected alert pop-up is displayed and you have not addressed this type of alert in an automated test case for similar pages, your script will immediately fail.

We must specifically manage these unanticipated alerts, and we may do so by utilising the try-catch block.

PopUps and Alerts in Selenium 

Note: If we write straight code (without try-catch) to accept or reject the alert and if the alert does not display, then our test case will be unable to throw any Selenium exception, such as a timeout exception. Try catch blocks can handle both situations.


In conclusion, alerts are little pop-up windows that show messages or notifications, advise the user of important information, or may even request permission to perform specific operations. Web/Javascript/browser-based alerts and Windows/OS-based alerts are the two different categories of alerts in Selenium. Web-based alerts can also be divided into confirmation alerts, prompt alerts, and simple alerts. Online application forms, banking websites, and social networking and email service provider websites like Gmail all prominently display these alerts. Such alerts must have appeared for every QA at some point while automating the program under test. A good automation testing training will explain more about Alerts and Pop-ups.

Facebook Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles

Back to top button