0
0
Selenium Pythontesting~20 mins

Simple alert acceptance in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Alert Acceptance Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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()
ATimeoutException
BUnhandledAlertException
CNoSuchElementException
DAlert accepted
Attempts:
2 left
💡 Hint
Think about what happens after alert.accept() is called and what the print statement does.
assertion
intermediate
1: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
Aassert alert_text == 'Warning!'
Bassert alert.text = 'Warning!'
Cassert alert_text.equals('Warning!')
Dassert alert_text != 'Warning!'
Attempts:
2 left
💡 Hint
Remember how to compare strings in Python assertions.
locator
advanced
1: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?
Adriver.find_element(By.CSS_SELECTOR, "button#show-alert")
Bdriver.find_element(By.ID, "show-alert")
Cdriver.find_element(By.XPATH, "//button[text()='Show Alert']")
Ddriver.find_element(By.CLASS_NAME, "alert-button")
Attempts:
2 left
💡 Hint
The button has no id or class, but has visible text.
🔧 Debug
advanced
2: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?
AThe alert was not switched to before calling accept()
BThe alert text is empty
CThe driver is not initialized
DThe button was not clicked
Attempts:
2 left
💡 Hint
You must switch to the alert before accepting it.
framework
expert
3: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
A
@pytest.fixture
def accept_alert_before_test(driver):
    button = driver.find_element(By.ID, 'alert-btn')
    button.click()
    alert = Alert(driver)
    alert.accept()
    yield
B
@pytest.fixture
def accept_alert_before_test(driver):
    button = driver.find_element(By.ID, 'alert-btn')
    button.click()
    alert = driver.switch_to.alert
    alert.accept()
    yield
C
@pytest.fixture
def accept_alert_before_test(driver):
    button = driver.find_element(By.ID, 'alert-btn')
    button.click()
    alert = driver.switch_to.alert
    alert.dismiss()
    yield
D
@pytest.fixture
def accept_alert_before_test(driver):
    button = driver.find_element(By.ID, 'alert-btn')
    button.click()
    alert = Alert(driver)
    alert.dismiss()
    yield
Attempts:
2 left
💡 Hint
Use driver.switch_to.alert to get the alert object before accepting.