Handle PopUps and Alerts in Selenium WebDriver

Definitely, all of us have come across alerts and PopUps while interacting with web applications. For example, whenever we try to delete something from an application we get alerts to confirm the delete operation. When we submit the payee details and add a payee to transfer funds we use to get a confirmation message.

When we click the OK button it allows us to proceed further but if you click on the Cancel button the operation is gets canceled. Most of the time these alerts or PopUps are created using JavaScript and they are not web elements. Because of this, you can’t inspect their properties using developer/inspect tools available in any of the browsers.

If you are new to Selenium and you are in the initial phase of learning and you encounter such alerts or popups you will get stuck. So in this tutorial, I will let you know how to handle PopUps and Alerts in Selenium WebDriver.

So first try to understand different types of HTML Alerts.

Types of Alerts

Alert Box with the OK Button Only

Some of the alerts have only one button like the OK button as shown below.

PopUps and Alerts in Selenium WebDriver

Alert Box with Two Buttons

Some of the alerts have two buttons. For example, OK and the Cancel button as shown below.

Handling Alert with two buttons in Selenium WebDriver

Prompt Window with one Input Text Field

Sometimes we may have to interact with prompt windows that require user input to be entered into it as shown below.

Handling Prompt box with input text field in Selenium WebDriver

Handle PopUps and Alerts in Selenium WebDriver

So here we go. Handling PopUps and alerts are not at all a complex task. Let’s see how to handle different types of PopUps / Alerts in Selenium WebDriver one by one.

Handle Alert and Get its Text In Selenium

Test Scenario

How to Automate the Scenario?

I know you will be fine until clicking on the “Try it” button. But what after the alert box is displayed. How to handle it?

Automation Approach to Handle Alerts / PopUps

Whenever we open an application in Selenium the focus of the web driver remains on the current page. If any action triggers an alert/PopUp Selenium will not be able to perform any operation on the Alert/PopUp until the web driver focus is shifted on it. Once the focus is shifted to the Alert/PopUp we can perform the desired action on it. So keep remembering the following key methods available in Selenium to deal with any type of Alert / PopUp window.

driver.switchTo().alert()It will switch to alert and return alert class object.
accept()It clicks on the ‘OK’ button of the alert box.It returns nothing.

driver.switchTo( ).alert( ).accept();
dismiss()It clicks on the ‘Cancel’ button of the alert box.It returns nothing.

driver.switchTo( ).alert( ).dismiss();
getText()It retrieves the text message of the alert box. It returns a String value.

String alertText=driver.switchTo( ).alert( ).getText();
sendKeys()It enters data into the alert box.


Example of Clicking on the OK Button on an Alert Box

Example of Clicking on the Cancel Button on an Alert Box

Handle Prompt Window In Selenium

Test Scenario

  1. Launch the browser
  2. Open the page https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_prompt
  3. Enter a name in the input field on the prompt box
  4. Click on the OK button

Example of entering text in the input field and click on the OK on a Prompt Window


We have learned how to handle alert/popups and prompt window in Selenium WebDriver.If you have any suggestions, comments, or queries please do mention them in the comment box and don’t forget to share this artcile.

Recommended Posts

Leave a Reply