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
- Click the sequential alerts button, located by its label.
- Switch to the first alert, keep its text in a
String, and accept it. - Wait for the second alert with
alertIsPresent(), keep its text in anotherString, and accept it. - Print the two messages with the labels
Alert 1:andAlert 2:.
Common mistakes
Expecting the old Alert to still show the first message
After
accept(), the sameAlertobject reads whatever dialog is open now, so a secondgetText()on it returns the second message. Store the first text before accepting.Touching the page between the alerts
Any page command, such as
findElement()orgetTitle(), while the second alert is open throwsUnhandledAlertExceptionand 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 throwNoAlertPresentException.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.
Related lessons
- Handle Simple AlertThe single-alert version of this flow.
- Explicit Wait - Element VisibleHow
WebDriverWaitanduntil()work, applied here to alerts. - Switch to Frame by IDSwitch into a frame, the next
switchTo()target.
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.
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();
}
}