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
- Find the Show Alert button by its visible text and click it.
- Switch to the alert and keep the
Alertobject in a variable. - Print the alert text after its label. Do this before closing it.
- 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_elementraisesUnexpectedAlertPresentException, and Chrome closes the dialog.Calling the property
Writing
driver.switch_to.alert()fails withTypeError: 'Alert' object is not callable. It is an attribute access, likedriver.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.
Related lessons
- Accept and Dismiss a ConfirmAdds the Cancel choice with
dismiss(). - Handle Two Alerts in a RowHandles two alerts that open back to back.
- Read a Modal Without Treating It as an AlertShows a popup that looks like an alert but is an ordinary HTML element.
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")
# Click Show Alert, print the alert text, and accept it
finally:
driver.quit()