About this lesson
Selenium Python confirm alert: press OK or Cancel with accept() and dismiss(), then check the page
Selenium Python AssignmentsIntermediateAlerts and Popups
How it works
A confirm dialog has two buttons, and the Alert object has one method for each. accept() presses OK. dismiss() presses Cancel. The page writes the outcome of each choice into a result element, so you can prove which button was pressed by reading that element.
The result element is a normal HTML element, so you read it with find_element and its text attribute, but only when no alert is open. While the dialog is up, the page is blocked, and the lookup fails with UnexpectedAlertPresentException. Chrome also closes the dialog in that case, so your choice is lost. Always close the dialog first, then read the result.
The exercise opens the confirm twice. After the first dialog closes, the Show Confirm button is still on the page, so a reference to it is still valid and you can click it again without searching a second time. The old Alert object would follow the new dialog, but calling driver.switch_to.alert again makes the second step clearer. Keep the text of each dialog tied to its own step, and switch again before you use the second one.
About the assignment
Open the confirm dialog twice. Accept the first one and read the page result. Dismiss the second and read the result again.
What you will do
- Click the Show Confirm button, found by its visible text.
- Switch to the alert, print its text after the first label, and accept it.
- Read the confirm result element and print it after its label.
- Click the button again, switch to the alert once more, dismiss it, and print the new result after the last label.
Common mistakes
Reading the result while the dialog is open
The lookup raises
UnexpectedAlertPresentExceptionwith the alert text in its message, and the dialog is closed for you, which breaks the sequence.Reusing the old alert variable
Right after
accept()no dialog is open, so callingdismiss()on the oldAlertobject raisesNoAlertPresentException: no such alert. Click the button first, then switch again.Mixing up the two buttons
OK is
accept(), Cancel isdismiss(). The result text differs for each, so swapped calls print the wrong word.
In a real test suite
Teams usually test both branches of a confirmation, such as deleting a record, with pytest.mark.parametrize over a pair of values: one for accept and one for dismiss, and an expected message for each. The test body then picks the method from the parameter.
The next lesson uses the third kind of dialog, which also accepts typed text.
Related lessons
- Accept a Simple AlertThe simple alert, where only
accept()is needed. - Type into a PromptA dialog with a text box, where the choice also carries typed input.
- Handle Two Alerts in a RowChains two different dialogs, one opening after the other is closed.
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")
# Accept one confirm, then dismiss a second one
finally:
driver.quit()