---Advertisement---

Handling Alerts and Popups in Selenium WebDriver: A Complete Guide

By Shiva

Updated On:

---Advertisement---
  • Mastering Alerts & Popups in Selenium WebDriver: A Complete Guide.
  • How to Handle JavaScript Alerts & Popups in Selenium Like a Pro.
  • Selenium WebDriver Alerts Handling: Best Practices & Examples.
  • Step-by-Step Guide to Managing Popups in Selenium WebDriver.
  • Handling Browser Alerts in Selenium: Everything You Need to Know.

In web automation testing using Selenium WebDriver, handling alerts and popups is a crucial skill. Web applications frequently use JavaScript-based alerts, confirmation boxes, and prompt dialogs to interact with users. As testers, understanding how to efficiently manage these alerts ensures smooth execution of automated test scripts.

This guide will walk you through various types of alerts in Selenium WebDriver and how to handle them using different methods. We will also cover how to manage popup windows and switch between multiple windows.


An alert in Selenium is a small JavaScript-based message box that appears on the screen. It provides users with important information, warnings, or requests for input. Alerts can be broadly categorized into three types:

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

Each type of alert requires different handling techniques, which we will explore in this tutorial.


A simple alert displays an informational message and usually contains only an “OK” button.

// Switching to Alert
Alert alert = driver.switchTo().alert();

// Capturing alert message
String alertMessage = alert.getText();
System.out.println("Alert message: " + alertMessage);

// Accepting (Clicking OK) on alert
alert.accept();

A confirmation alert asks the user for confirmation before proceeding. It has two options: OK and Cancel.

// Switching to alert
Alert confirmationAlert = driver.switchTo().alert();

// Getting alert text
String alertText = confirmationAlert.getText();
System.out.println("Confirmation Alert Text: " + alertText);

// Accepting alert (Clicking OK)
confirmationAlert.accept();

// Dismissing alert (Clicking Cancel)
// confirmationAlert.dismiss();

A prompt alert asks the user for input. It has an input box along with “OK” and “Cancel” buttons.

// Switching to alert
Alert promptAlert = driver.switchTo().alert();

// Entering text in alert
promptAlert.sendKeys("Test Input");

// Accepting alert
promptAlert.accept();

Apart from alerts, popups can appear in different forms like authentication popups, file upload dialogs, and advertisement popups. Selenium WebDriver provides different approaches to handling them.

Authentication popups require entering a username and password. These can be handled by embedding credentials in the URL.

// Format: http://username:password@URL
String URL = "http://admin:admin@the-internet.herokuapp.com/basic_auth";
driver.get(URL);

Sometimes clicking a button opens a new browser window. Selenium provides methods to switch between multiple windows.

// Get parent window handle
String parentWindow = driver.getWindowHandle();

// Click on a button that opens a new window
driver.findElement(By.id("open-new-window")).click();

// Get all window handles
Set<String> windowHandles = driver.getWindowHandles();
for (String windowHandle : windowHandles) {
    if (!windowHandle.equals(parentWindow)) {
        driver.switchTo().window(windowHandle);
        System.out.println("Switched to new window");
    }
}

// Close the new window and switch back to the main window
driver.close();
driver.switchTo().window(parentWindow);

  1. Use Explicit Waits: Sometimes alerts take time to appear. Use WebDriverWait to handle them efficiently.WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); wait.until(ExpectedConditions.alertIsPresent());
  2. Handle Unexpected Alerts: Unexpected alerts may break the script. Use try-catch blocks to manage them. try { Alert unexpected Alert = driver. switch To().alert(); unexpected Alert .dismiss(); } catch (No Alert Present Exception e) { System .out. print ln("No alert found"); }
  3. Verify Alert Messages: Always validate alert messages to ensure they contain expected text before proceeding.
  4. Use Appropriate Switching Methods: Switch back to the main window after handling alerts or popups.

Handling alerts and popups is a key aspect of Selenium automation testing. By understanding different types of alerts—simple, confirmation, and prompt—and using the appropriate Selenium WebDriver methods, you can efficiently manage popups and alerts in your test scripts. Implementing best practices like explicit waits, exception handling, and window switching will enhance the reliability of your automation scripts.

Leave a Comment

Index