0
0
Selenium Pythontesting~20 mins

Handling pop-up windows in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pop-up Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Selenium code snippet?
Consider the following Selenium Python code that handles a simple alert pop-up. What will be printed after execution?
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.alert import Alert

# Assume driver is already initialized and navigated to page
alert_button = driver.find_element(By.ID, 'alertBtn')
alert_button.click()
alert = Alert(driver)
alert_text = alert.text
alert.accept()
print(alert_text)
ANo output, code raises NoAlertPresentException
BCode raises ElementNotInteractableException
CEmpty string printed
DThe text message shown in the alert pop-up
Attempts:
2 left
💡 Hint
Remember that Alert(driver) switches context to the alert pop-up and .text reads its message.
assertion
intermediate
1:30remaining
Which assertion correctly verifies a confirmation pop-up was accepted?
After clicking a button that triggers a confirmation pop-up, you accept it. Which assertion correctly checks that the pop-up was accepted by verifying the page text?
Selenium Python
from selenium.webdriver.common.by import By

# After accepting confirmation pop-up
result_text = driver.find_element(By.ID, 'result').text
Aassert 'Error' in result_text
Bassert result_text == 'Cancelled'
Cassert 'Confirmed' in result_text
Dassert result_text == ''
Attempts:
2 left
💡 Hint
Accepting confirmation usually changes page text to indicate confirmation.
locator
advanced
1:30remaining
Which locator is best to find a button that opens a new browser window?
You want to locate a button that opens a new window. The button has a unique aria-label 'Open new window'. Which locator is best practice?
Adriver.find_element(By.ID, 'openWindowBtn')
Bdriver.find_element(By.CSS_SELECTOR, 'button[aria-label="Open new window"]')
Cdriver.find_element(By.CLASS_NAME, 'btn-open')
Ddriver.find_element(By.XPATH, '//button[text()="Open new window"]')
Attempts:
2 left
💡 Hint
Use semantic attributes for accessibility and uniqueness.
🔧 Debug
advanced
2:00remaining
Why does this Selenium code fail to switch to the new window?
This code clicks a button to open a new window, then tries to switch to it. Why does it fail?
Selenium Python
original_window = driver.current_window_handle
button = driver.find_element(By.ID, 'openWindowBtn')
button.click()
# Attempt to switch
for handle in driver.window_handles:
    if handle != original_window:
        driver.switch_to.window(handle)
        break
print(driver.title)
AThe new window is not fully opened before switching; need to wait
BThe loop incorrectly switches back to original window
Cdriver.window_handles is empty after click
Ddriver.switch_to.window() requires window name, not handle
Attempts:
2 left
💡 Hint
New windows may take time to open; immediate switch can fail.
framework
expert
2:30remaining
Which test framework feature best handles flaky pop-up alerts in Selenium tests?
Pop-up alerts sometimes appear with delay causing test failures. Which framework feature best handles this to make tests stable?
AUse explicit waits to wait for alert presence before interacting
BUse implicit waits globally to wait for all elements including alerts
CWrap alert handling code in try-except without waits
DDisable pop-ups in browser settings to avoid alerts
Attempts:
2 left
💡 Hint
Waiting specifically for alerts is more reliable than global waits.