How to handle random modal windows in Selenium

Many sites use modal windows for sales events, announcements and user surveys.

Especially in the last 2 months of the year when there are so many holidays (Christmas, New Year’s Eve, Boxing Day).


A modal is an HTML element that is displayed above the current page.

It includes

  • title
  • description
  • action button (take a survey, visit on sale section, etc)
  • close button

The user cannot interact with the main page until the modal is closed.

Modals can be a problem for test automation scripts.

If the modal is displayed consistently on a page, it is easy for the automation script to interact with it (take the action suggested by the modal or close it).

But if the modal is displayed randomly, the test automation script needs to be able to deal with the modal when displayed and not displayed.

How do we interact with random modal windows?

Let’s see how to do it for a simple web page.

The web page displays initially a button:


When clicking the button, the modal is not displayed some times.

In these cases, the page displays the “popup not displayed” and “done” messages:


Other times, when clicking the button, the modal is displayed:


After closing the modal, the page displays “done”.


The sample code consists in 3 classes:

  • test class – includes setUp() and tearDown() methods and the test script
  • main class – implements the interactions with the main page
  • popup class – implements the interaction with the popup


import static org.testng.AssertJUnit.assertTrue;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class TestClass {

WebDriver driver;

public void setUp()
driver = new ChromeDriver();

public void tearDown() {

@Test(invocationCount = 10)
public void testRandomPopup() {
MainPage mainPage = new MainPage(driver);;

Popup popup = mainPage.displayPopup();



The test script does the following

  • opens the main page
  • clicks the button that may display the popup
  • closes the popup; if the popup is displayed, then it is closed; if the popup is not displayed, nothing happens
  • asserts that the work is done


import org.openqa.selenium.By;
import org.openqa.selenium.NotFoundException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class MainPage {
  private final WebDriver driver;
  private final By doneStatusId ="done");
  private final By displayPopupId ="displayPopup");
  public MainPage(WebDriver driver) {
    this.driver = driver;
  public void open() throws InterruptedException {
    if (driver.getTitle().contains("Test Random Popup") == false)
        throw new NotFoundException("main page not displayed");
  public String done() {
    WebElement doneLabel = driver.findElement(doneStatusId);
    return doneLabel.getText();
  public Popup displayPopup() {
    WebElement displayButton = driver.findElement(displayPopupId);;
    return new Popup(driver);

The MainPage class has the following methods:

  • open() – opens the page and checks that the opened page is the correct one
  • displayPopup() – clicks the button that may display the popup
  • done() – returns the message displayed after the popup is handled


import org.openqa.selenium.By;
import org.openqa.selenium.InvalidElementStateException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import static*;
public class Popup {
  private final WebDriverWait wait;
  private final By closeElementLocator ="closePopup");
  private final By popupIdLocator ="popupId");
  public Popup(WebDriver driver) {
    this.wait = new WebDriverWait(driver, 5);
  public void close() throws InterruptedException {
    if (isDisplayed()) {
        WebElement closeElement = wait.until(
  private Boolean isDisplayed() throws InterruptedException {
     try {
        WebElement popup = wait.until(
        return popup.isDisplayed();
     catch (Exception ex) {
       return false;

The popup class has 2 methods:

1. isDisplayed()

This is the most important method for managing the random modal.

It tries finding the modal element.

If the modal element is found, isDisplayed() returns true if modal is displayed and false otherwise.

If the popup is not displayed, an exception is thrown.

The exception is caught and isDisplayed() returns false;

2. close()

It closes the popup if it is displayed.

Thanks for reading.

Want to get started with Selenium WebDriver and Java in 10 short lessons?


One Comment

  1. Hello,

    Thanks for the article. How can we handle a situation where a random popup/alert/error may appear anywhere on the page at anytime? I would want to close that and move ahead with my testing!



Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s