Confirmation alert handling in Selenium Python - Build an Automation Script
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 from selenium.common.exceptions import TimeoutException from selenium.webdriver.common.alert import Alert import unittest class TestConfirmationAlertHandling(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.get('https://example.com/items') # Replace with actual URL self.wait = WebDriverWait(self.driver, 10) def test_delete_item_confirmation_alert(self): driver = self.driver wait = self.wait # Locate the delete button for the first item delete_button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.item .delete-btn'))) delete_button.click() try: # Wait for alert to be present alert = wait.until(EC.alert_is_present()) # Verify alert text alert_text = alert.text self.assertEqual(alert_text, 'Are you sure you want to delete this item?') # Accept the alert alert.accept() # Verify the item is removed from the list # Wait until the first item is no longer present or list updates wait.until(EC.invisibility_of_element_located((By.CSS_SELECTOR, '.item'))) # Optionally, check the count of items decreased items = driver.find_elements(By.CSS_SELECTOR, '.item') self.assertTrue(len(items) < 1, 'Item was not deleted') except TimeoutException: self.fail('Confirmation alert did not appear') def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main()
This test script uses Selenium with Python's unittest framework to automate the confirmation alert handling.
setUp: Opens the browser and navigates to the items page.
test_delete_item_confirmation_alert: Clicks the delete button on the first item, waits explicitly for the alert, verifies the alert text, accepts the alert, then waits for the item to be removed from the list and asserts the deletion.
Explicit waits ensure the test waits only as long as needed for elements or alerts, avoiding fixed delays.
tearDown: Closes the browser after the test.
This approach follows best practices by using explicit waits, proper locators, and handling exceptions to make the test reliable and maintainable.
Now add data-driven testing with 3 different items to delete, verifying alert handling for each.
Practice
Solution
Step 1: Switch to the alert
Usedriver.switch_to.alertto access the alert popup.Step 2: Accept the alert
Callaccept()on the alert object to confirm it.Final Answer:
driver.switch_to.alert.accept() -> Option CQuick Check:
Accept alert = driver.switch_to.alert.accept() [OK]
- Using dismiss() instead of accept() to confirm
- Calling accept() directly on driver without switch_to.alert
- Using incorrect method names like confirm.accept()
Solution
Step 1: Access the alert
Usedriver.switch_to.alertto get the alert object.Step 2: Call dismiss() method
Usedismiss()to cancel or reject the confirmation alert.Final Answer:
driver.switch_to.alert.dismiss() -> Option BQuick Check:
Dismiss alert = driver.switch_to.alert.dismiss() [OK]
- Calling dismiss() on driver directly
- Using wrong method order like switch_to.dismiss()
- Using driver.alert.dismiss() which is invalid
driver.find_element(By.ID, 'confirmBtn').click()
alert = driver.switch_to.alert
alert.accept()
print('Alert accepted')Solution
Step 1: Click triggers confirmation alert
The button click opens a confirmation alert popup.Step 2: Accept the alert and print message
The alert is accepted usingalert.accept(), then prints 'Alert accepted'.Final Answer:
Alert accepted -> Option AQuick Check:
Accept alert prints confirmation message [OK]
- Expecting alert text to print instead of message
- Using dismiss() but expecting accept output
- Not switching to alert before accept()
driver.find_element(By.ID, 'confirmBtn').click() alert = driver.alert alert.dismiss()
Solution
Step 1: Check alert access method
The code usesdriver.alert, which is invalid in Selenium Python.Step 2: Correct way to access alert
Usedriver.switch_to.alertto get the alert object before calling dismiss().Final Answer:
Incorrect alert access; should use driver.switch_to.alert -> Option AQuick Check:
Alert access = driver.switch_to.alert [OK]
- Using driver.alert instead of driver.switch_to.alert
- Calling dismiss() on wrong object
- Ignoring import errors unrelated to alert handling
Solution
Step 1: Access alert correctly
Usedriver.switch_to.alertto get the alert object.Step 2: Check alert text property
Usealert.text(property, not method) to get alert message and compare.Step 3: Accept alert if text matches
If text matches 'Are you sure?', callalert.accept()to confirm.Final Answer:
alert = driver.switch_to.alert if alert.text == 'Are you sure?': alert.accept() -> Option DQuick Check:
Use alert.text property and accept() method [OK]
- Using alert.text() as a method instead of property
- Using driver.alert instead of driver.switch_to.alert
- Calling dismiss() when accept() is needed
Ready to prove it?
Pass the test and this pattern moves unknown
