About this lesson
Selenium Python multiple alerts: handle two alerts that open one after the other
Selenium Python AssignmentsIntermediateAlerts and Popups
How it works
One click can lead to more than one dialog. The page here runs two alerts in a row, and the second one is only opened after the first is closed. Browsers show one dialog at a time, so there is never a moment where both are open. Your script has to treat them as two separate events: switch, read, accept, then switch again.
Each call to driver.switch_to.alert gives a view of whichever dialog is open at that moment. An Alert variable kept from the first dialog even reads the second dialog text after the first accept(), because it points at the current dialog and not a fixed one. That is easy to misread, so use a new variable per dialog and keep each text tied to its step.
Once both are closed, asking for an alert raises NoAlertPresentException, a handy end check that nothing is left open. Here the second dialog was ready the instant the first closed. On a slower page you may need WebDriverWait with alert_is_present between the steps.
About the assignment
Click one button that produces two alerts, then print the message of each and close both.
What you will do
- Click Show Sequential Alerts, found by its visible text.
- Switch to the first alert, print its text after the first label, and accept it.
- Switch to the alert again, which is now the second dialog, and print its text after the second label.
- Accept the second alert so nothing stays open.
Common mistakes
Accepting before reading
The first text is lost when the dialog closes. A later read silently returns the second alert's text, so your labels end up wrong.
Stopping after the first dialog
The second alert stays open, and most driver calls, even
driver.title, raiseUnexpectedAlertPresentException.Switching too early
On a slow machine the second dialog may not be open yet. The switch then raises
NoAlertPresentException. A wait withalert_is_presentfixes it.
In a real test suite
A typical suite wraps this in a function that loops while an alert is present, collects each message into a list, and accepts as it goes. The test then compares the list to the expected messages, which works for any number of dialogs.
The next lesson shows a popup that looks like an alert to a person but is not one to Selenium at all.
Related lessons
- Accept a Simple AlertHandles a single alert, the building block used twice here.
- Accept and Dismiss a ConfirmOpens a dialog twice in a row, with a different choice each time.
- Read a Modal Without Treating It as an AlertContrasts browser dialogs with a popup built from HTML.
To try one of these calls on any page without a task, open the Online Selenium Python Compiler.
Starter code
The editor above opens with this code. Fill in the TODO lines, then run and submit it.
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
try:
driver.get("https://testkru.com/Interactions/Alerts")
# Read and accept both sequential alerts
finally:
driver.quit()