Skip to content
CodeKruPro

Handle Sequential Alerts

43/70

Handle Sequential Alerts

Sometimes multiple alerts appear one after another. You must handle each one in sequence.

On the Alerts page, clicking "Show Sequential Alerts" triggers two alerts in a row.

Task:

  1. Navigate to https://testkru.com/Interactions/Alerts
  2. Click "Show Sequential Alerts"
  3. Handle the first alert - get text and accept
  4. Handle the second alert - get text and accept
  5. Print both alert texts

Expected output:

Alert 1: This is the first alert!
Alert 2: This is the second alert after the first is closed!

About this lesson

Handle multiple alerts in sequence in Selenium Java

Learn Selenium JavaIntermediateAlerts & Popups

How it works

Some actions open one dialog after another: the page shows a second alert only once the first is closed. Each dialog needs its own read and accept. An Alert is not tied to one dialog: it always acts on whichever dialog is open now. Switching again for each dialog keeps the code clear about which one you are reading.

Here the second alert opens as soon as the first is accepted, but in real apps a follow-up dialog often comes after a delay or a network call, so waiting is the safer habit. new WebDriverWait(driver, Duration.ofSeconds(5)).until(ExpectedConditions.alertIsPresent()) waits for it and returns the Alert.

After the second alert is accepted, the sequence ends. Switching again throws NoAlertPresentException, which is a simple way to check that no extra dialog appeared.

About the assignment

One button on the Alerts page opens two alerts in a row. You handle both, keeping each message, and print the two messages once the page is free again.

What you will do

  1. Click the sequential alerts button, located by its label.
  2. Switch to the first alert, keep its text in a String, and accept it.
  3. Wait for the second alert with alertIsPresent(), keep its text in another String, and accept it.
  4. Print the two messages with the labels Alert 1: and Alert 2:.

Common mistakes

  • Expecting the old Alert to still show the first message

    After accept(), the same Alert object reads whatever dialog is open now, so a second getText() on it returns the second message. Store the first text before accepting.

  • Touching the page between the alerts

    Any page command, such as findElement() or getTitle(), while the second alert is open throws UnhandledAlertException and the alert is dismissed. Keep the strings and print at the end.

  • Switching without a wait in real apps

    When a follow-up dialog depends on a delay or a request, an immediate switchTo().alert() can run before it opens and throw NoAlertPresentException. alertIsPresent() waits for it.

In a real test suite

Chained dialogs appear in older checkout and admin flows, for example a warning followed by a confirmation. A small helper that waits for an alert, returns its text and accepts it keeps such tests short.

Lesson 42 typed into a prompt. Lesson 44 moves to another switchTo() target, frames, where the page itself contains other pages.

To try one of these calls on any page without a task, open the Online Selenium Java Compiler.

Starter code

The editor above opens with this code. Fill in the TODO lines, then run and submit it.

Main.java
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

public class Main {
    public static void main(String[] args) throws Exception {
        WebDriver driver = new ChromeDriver();
        driver.get("https://testkru.com/Interactions/Alerts");
        
        // TODO: Click "Show Sequential Alerts"
        // TODO: Handle first alert
        // TODO: Handle second alert
        // TODO: Print both alert texts
        
        driver.quit();
    }
}