Skip to content
CodeKruPro

Accept and Dismiss a Confirm

45/80

Accept and Dismiss a Confirm

A confirm has two choices. accept() is OK. dismiss() is Cancel. The page writes the choice into #confirmResult.

Task:

  1. Click Show Confirm, print the alert text, and accept it
  2. Print the result
  3. Open the confirm again, dismiss it, and print the result

Print the lines below exactly.

Expected output:

Alert text: Do you want to proceed?
Result: You clicked: OK
Dismissed: You clicked: Cancel

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

  1. Click the Show Confirm button, found by its visible text.
  2. Switch to the alert, print its text after the first label, and accept it.
  3. Read the confirm result element and print it after its label.
  4. 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 UnexpectedAlertPresentException with 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 calling dismiss() on the old Alert object raises NoAlertPresentException: no such alert. Click the button first, then switch again.

  • Mixing up the two buttons

    OK is accept(), Cancel is dismiss(). 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.

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")
    # Accept one confirm, then dismiss a second one
finally:
    driver.quit()