Skip to content
CodeKruPro

Handle Two Alerts in a Row

47/80

Handle Two Alerts in a Row

The second alert appears only after you accept the first. Read and accept them one at a time.

Task:

  1. Click Show Sequential Alerts
  2. Print and accept the first alert
  3. Print and accept the second alert

Print the lines below exactly.

Expected output:

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

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

  1. Click Show Sequential Alerts, found by its visible text.
  2. Switch to the first alert, print its text after the first label, and accept it.
  3. Switch to the alert again, which is now the second dialog, and print its text after the second label.
  4. 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, raise UnexpectedAlertPresentException.

  • Switching too early

    On a slow machine the second dialog may not be open yet. The switch then raises NoAlertPresentException. A wait with alert_is_present fixes 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.

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.

main.py
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()