Skip to content
CodeKruPro

Accept a Simple Alert

44/80

Accept a Simple Alert

A browser alert is not an element. driver.switch_to.alert is the alert. Read its text, then accept(). The button has no id. Find it by the visible text Show Alert.

Task:

  1. Click Show Alert
  2. Print the alert text
  3. Accept the alert

Print the lines below exactly.

Expected output:

Alert text: This is a simple alert!

About this lesson

Selenium Python: read and accept a simple alert with switch_to.alert

Selenium Python AssignmentsIntermediateAlerts and Popups

How it works

A browser alert is not part of the page. It belongs to the browser, so none of the find_element calls can see it. To work with it you ask the driver to switch focus: driver.switch_to.alert returns an Alert object. In Python this is a property, so there are no brackets after alert. People coming from Java often add them, and Python then complains that an Alert object is not callable.

The Alert object has a text attribute with the message and an accept() method that presses OK. Read the text first. Once you accept, the alert is gone, and reading text afterwards raises NoAlertPresentException with the message "no such alert". Asking for switch_to.alert when none is open raises the same exception, so that name is worth recognising.

While an alert is open, most other driver calls are blocked. Asking for the title, for instance, raises UnexpectedAlertPresentException, and by default Chrome closes the alert as part of that failure. Your script then loses the message it wanted to read. The button on this page has no id, so locate it by the words shown on it, using an XPath that matches on its visible text.

About the assignment

Open the Alerts page, trigger the simple alert, print what it says and close it.

What you will do

  1. Find the Show Alert button by its visible text and click it.
  2. Switch to the alert and keep the Alert object in a variable.
  3. Print the alert text after its label. Do this before closing it.
  4. Call the method that accepts the alert, so the page is usable again.

Common mistakes

  • Treating the alert as an element

    No locator will find it. With the alert open, find_element raises UnexpectedAlertPresentException, and Chrome closes the dialog.

  • Calling the property

    Writing driver.switch_to.alert() fails with TypeError: 'Alert' object is not callable. It is an attribute access, like driver.title.

  • Reading the text after accepting

    The alert no longer exists, so the read fails with NoAlertPresentException: no such alert. Print first, accept second.

In a real test suite

In a pytest suite an alert check usually sits inside a page object method such as get_alert_text(), with WebDriverWait and alert_is_present guarding against a click that is slow to open the dialog. That condition returns the Alert object itself once it exists.

The next lesson covers a dialog with two buttons and shows how to pick OK or Cancel.

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")
    # Click Show Alert, print the alert text, and accept it
finally:
    driver.quit()