Challenge - 5 Problems
Alert Acceptance Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output after accepting the alert?
Consider the following Selenium Python code that triggers a simple alert and accepts it. What will be printed after the alert is accepted?
Selenium Python
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.alert import Alert # Setup driver (assume driver is correctly initialized) driver = webdriver.Chrome() driver.get('data:text/html,<button onclick="alert(\'Hello!\')" id="btn">Click</button>') button = driver.find_element(By.ID, 'btn') button.click() alert = Alert(driver) alert.accept() print('Alert accepted') driver.quit()
Attempts:
2 left
💡 Hint
Think about what happens after alert.accept() is called and what the print statement does.
✗ Incorrect
The alert is accepted successfully, so the print statement executes and outputs 'Alert accepted'. No exceptions occur.
❓ assertion
intermediate1:30remaining
Which assertion correctly verifies alert text?
You want to verify that a simple alert shows the text 'Warning!'. Which assertion is correct after switching to the alert?
Selenium Python
alert = driver.switch_to.alert alert_text = alert.text
Attempts:
2 left
💡 Hint
Remember how to compare strings in Python assertions.
✗ 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 strings. Option A asserts the opposite.
❓ locator
advanced1:30remaining
Choose the best locator to find a button triggering a simple alert
You have a button that triggers a simple alert when clicked. The button has the text 'Show Alert' but no id or class attributes. Which locator is best to find this button?
Attempts:
2 left
💡 Hint
The button has no id or class, but has visible text.
✗ Incorrect
Option C uses XPath to find a button by its exact text, which works when no id or class is present. Options A, C, and D rely on id or class attributes that do not exist.
🔧 Debug
advanced2:00remaining
Why does alert.accept() raise UnexpectedAlertPresentException?
You run this code:
button.click()
alert.accept()
But get UnexpectedAlertPresentException on alert.accept(). What is the likely cause?
Attempts:
2 left
💡 Hint
You must switch to the alert before accepting it.
✗ Incorrect
UnexpectedAlertPresentException occurs if you try to interact with the page while an alert is open and you have not switched to it. You must switch to the alert first.
❓ framework
expert3:00remaining
Which pytest fixture correctly handles alert acceptance for multiple tests?
You want to create a pytest fixture that clicks a button triggering a simple alert and accepts it before each test. Which fixture code is correct?
Selenium Python
import pytest from selenium.webdriver.common.by import By from selenium.webdriver.common.alert import Alert @pytest.fixture def accept_alert_before_test(driver): button = driver.find_element(By.ID, 'alert-btn') button.click() alert = Alert(driver) alert.accept() yield # Tests use this fixture
Attempts:
2 left
💡 Hint
Use driver.switch_to.alert to get the alert object before accepting.
✗ Incorrect
Option B correctly switches to the alert using driver.switch_to.alert and accepts it. Option B uses Alert(driver) which may not always work reliably. Options B and C dismiss the alert instead of accepting it.