Challenge - 5 Problems
Confirmation Alert Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Selenium Python code handling a confirmation alert?
Consider the following Selenium Python code snippet that clicks a button to trigger a confirmation alert, then accepts it. What will be printed?
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 # Assume driver is already initialized and navigated to the page button = driver.find_element(By.ID, 'confirmBtn') button.click() alert = WebDriverWait(driver, 5).until(EC.alert_is_present()) alert.accept() print('Alert accepted')
Attempts:
2 left
💡 Hint
Think about what happens after the alert is accepted and the print statement runs.
✗ Incorrect
The code waits for the alert to appear, accepts it, then prints 'Alert accepted'. No exceptions occur if the alert is present and handled correctly.
❓ assertion
intermediate1:30remaining
Which assertion correctly verifies the alert text before accepting it?
You want to check that the confirmation alert text is exactly 'Are you sure?' before accepting it. Which assertion is correct?
Selenium Python
alert = driver.switch_to.alert
alert_text = alert.text
# Which assertion below is correct?Attempts:
2 left
💡 Hint
Remember how to compare strings in Python.
✗ Incorrect
Option A uses the correct equality operator '==' to compare strings in Python. Option A uses assignment '=' which is invalid in assert. Option A uses a method not available on Python strings. Option A asserts the opposite.
❓ locator
advanced2:00remaining
Which locator is best to find a button that triggers a confirmation alert?
You want to locate a button that triggers a confirmation alert. The button has the text 'Delete' and a class 'btn-danger'. Which locator is best practice?
Attempts:
2 left
💡 Hint
CSS selectors do not support :contains pseudo-class in Selenium.
✗ Incorrect
Option C uses XPath to match button text and class, which is precise. Option C uses a CSS selector with :contains which is not supported in Selenium. Option C uses an ID that does not exist. Option C selects all buttons with class 'btn-danger' but does not check text.
🔧 Debug
advanced2:00remaining
Why does this Selenium code raise UnexpectedAlertPresentException?
Review the code below. It tries to click a button and then get page title. Why does it raise UnexpectedAlertPresentException?
Selenium Python
button = driver.find_element(By.ID, 'confirmBtn') button.click() title = driver.title print(title)
Attempts:
2 left
💡 Hint
Think about what happens when an alert is present and you try to interact with the page.
✗ Incorrect
If a confirmation alert appears after clicking the button, Selenium blocks further commands until the alert is handled. Accessing driver.title without accepting or dismissing the alert causes UnexpectedAlertPresentException.
❓ framework
expert2:30remaining
In a pytest Selenium test, how to properly handle confirmation alerts to avoid flaky tests?
You write pytest tests using Selenium. Confirmation alerts appear unpredictably after clicking buttons. Which approach best avoids flaky tests?
Attempts:
2 left
💡 Hint
Think about reliable ways to detect alerts instead of guessing with fixed delays.
✗ Incorrect
Explicit waits for alerts ensure the test waits exactly until the alert appears, then handles it. Ignoring alerts or using fixed sleeps causes flaky tests. Disabling alerts may not be possible or realistic.