Bird
Raised Fist0
Selenium Pythontesting~10 mins

Confirmation alert handling in Selenium Python - Test Execution Trace

Choose your learning style10 modes available
Test Overview

This test opens a webpage with a confirmation alert, clicks a button to trigger the alert, accepts the alert, and verifies the confirmation message on the page.

Test Code - Selenium
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import unittest

class TestConfirmationAlert(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://example.com/confirmation-alert')

    def test_accept_confirmation_alert(self):
        driver = self.driver
        # Click the button that triggers the confirmation alert
        button = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.ID, 'confirm-btn'))
        )
        button.click()

        # Wait for the alert to be present
        alert = WebDriverWait(driver, 10).until(EC.alert_is_present())

        # Accept the confirmation alert
        alert.accept()

        # Verify the confirmation message is displayed
        message = WebDriverWait(driver, 10).until(
            EC.visibility_of_element_located((By.ID, 'confirm-message'))
        )
        self.assertEqual(message.text, 'You clicked OK!')

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Test starts and opens Chrome browserBrowser window opens at URL https://example.com/confirmation-alert—PASS
2Waits until the button with ID 'confirm-btn' is clickablePage fully loaded with visible button 'confirm-btn'Button is clickablePASS
3Clicks the 'confirm-btn' button to trigger confirmation alertConfirmation alert appears on screen—PASS
4Waits for the confirmation alert to be presentAlert dialog is present and activeAlert is presentPASS
5Accepts the confirmation alert by clicking OKAlert disappears, page updates accordingly—PASS
6Waits for the confirmation message element with ID 'confirm-message' to be visibleConfirmation message 'You clicked OK!' is visible on the pageMessage text equals 'You clicked OK!'PASS
7Test ends and browser closesBrowser window closed—PASS
Failure Scenario
Failing Condition: The confirmation alert does not appear after clicking the button
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test do after clicking the button with ID 'confirm-btn'?
AWaits for and accepts the confirmation alert
BImmediately checks the confirmation message without waiting
CCloses the browser
DClicks the button again
Key Result
Always wait explicitly for alerts before interacting with them to avoid timing issues and ensure reliable test execution.

Practice

(1/5)
1. What is the correct method to accept a confirmation alert in Selenium with Python?
easy
A. driver.confirm.accept()
B. driver.switch_to.alert.dismiss()
C. driver.switch_to.alert.accept()
D. driver.alert.accept()

Solution

  1. Step 1: Switch to the alert

    Use driver.switch_to.alert to access the alert popup.
  2. Step 2: Accept the alert

    Call accept() on the alert object to confirm it.
  3. Final Answer:

    driver.switch_to.alert.accept() -> Option C
  4. Quick Check:

    Accept alert = driver.switch_to.alert.accept() [OK]
Hint: Use switch_to.alert before accept() to handle confirmation [OK]
Common Mistakes:
  • Using dismiss() instead of accept() to confirm
  • Calling accept() directly on driver without switch_to.alert
  • Using incorrect method names like confirm.accept()
2. Which of the following is the correct syntax to dismiss a confirmation alert in Selenium Python?
easy
A. driver.alert.dismiss()
B. driver.switch_to.alert.dismiss()
C. driver.dismiss.alert()
D. driver.switch_to.dismiss()

Solution

  1. Step 1: Access the alert

    Use driver.switch_to.alert to get the alert object.
  2. Step 2: Call dismiss() method

    Use dismiss() to cancel or reject the confirmation alert.
  3. Final Answer:

    driver.switch_to.alert.dismiss() -> Option B
  4. Quick Check:

    Dismiss alert = driver.switch_to.alert.dismiss() [OK]
Hint: Dismiss confirmation alert with switch_to.alert.dismiss() [OK]
Common Mistakes:
  • Calling dismiss() on driver directly
  • Using wrong method order like switch_to.dismiss()
  • Using driver.alert.dismiss() which is invalid
3. What will be the output of the following code snippet if the confirmation alert is accepted?
driver.find_element(By.ID, 'confirmBtn').click()
alert = driver.switch_to.alert
alert.accept()
print('Alert accepted')
medium
A. Alert accepted
B. Alert dismissed
C. No output, code throws exception
D. Alert text printed

Solution

  1. Step 1: Click triggers confirmation alert

    The button click opens a confirmation alert popup.
  2. Step 2: Accept the alert and print message

    The alert is accepted using alert.accept(), then prints 'Alert accepted'.
  3. Final Answer:

    Alert accepted -> Option A
  4. Quick Check:

    Accept alert prints confirmation message [OK]
Hint: Accept alert then print confirmation message [OK]
Common Mistakes:
  • Expecting alert text to print instead of message
  • Using dismiss() but expecting accept output
  • Not switching to alert before accept()
4. Identify the error in the following code that tries to dismiss a confirmation alert:
driver.find_element(By.ID, 'confirmBtn').click()
alert = driver.alert
alert.dismiss()
medium
A. Incorrect alert access; should use driver.switch_to.alert
B. dismiss() method does not exist for alerts
C. Missing import for By module
D. No error; code is correct

Solution

  1. Step 1: Check alert access method

    The code uses driver.alert, which is invalid in Selenium Python.
  2. Step 2: Correct way to access alert

    Use driver.switch_to.alert to get the alert object before calling dismiss().
  3. Final Answer:

    Incorrect alert access; should use driver.switch_to.alert -> Option A
  4. Quick Check:

    Alert access = driver.switch_to.alert [OK]
Hint: Always use driver.switch_to.alert to access alerts [OK]
Common Mistakes:
  • Using driver.alert instead of driver.switch_to.alert
  • Calling dismiss() on wrong object
  • Ignoring import errors unrelated to alert handling
5. You want to verify the text of a confirmation alert before accepting it. Which code snippet correctly handles this scenario?
hard
A. alert = driver.switch_to.alert if alert.get_text == 'Are you sure?': alert.accept()
B. alert = driver.alert if alert.getText() == 'Are you sure?': alert.accept()
C. alert = driver.switch_to.alert if alert.text() == 'Are you sure?': alert.dismiss()
D. alert = driver.switch_to.alert if alert.text == 'Are you sure?': alert.accept()

Solution

  1. Step 1: Access alert correctly

    Use driver.switch_to.alert to get the alert object.
  2. Step 2: Check alert text property

    Use alert.text (property, not method) to get alert message and compare.
  3. Step 3: Accept alert if text matches

    If text matches 'Are you sure?', call alert.accept() to confirm.
  4. Final Answer:

    alert = driver.switch_to.alert if alert.text == 'Are you sure?': alert.accept() -> Option D
  5. Quick Check:

    Use alert.text property and accept() method [OK]
Hint: Use alert.text property to check text before accept() [OK]
Common Mistakes:
  • Using alert.text() as a method instead of property
  • Using driver.alert instead of driver.switch_to.alert
  • Calling dismiss() when accept() is needed
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard