Bird
Raised Fist0
Selenium Pythontesting~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the correct way to switch to a browser alert pop-up in Selenium with Python?
easy
A. driver.switch_to.window('alert')
B. driver.switch_to.alert
C. driver.switch_to.frame('alert')
D. driver.switch_to.default_content()

Solution

  1. Step 1: Understand alert handling in Selenium

    Alerts are special pop-ups that require switching to alert context using driver.switch_to.alert.
  2. Step 2: Identify the correct method to switch

    Using driver.switch_to.alert accesses the alert object directly, allowing interaction.
  3. Final Answer:

    driver.switch_to.alert -> Option B
  4. Quick Check:

    Alert pop-up switch = driver.switch_to.alert [OK]
Hint: Use driver.switch_to.alert for alerts, not window or frame [OK]
Common Mistakes:
  • Using switch_to.window for alerts
  • Trying to switch to alert as a frame
  • Not switching before interacting with alert
2. Which of the following is the correct syntax to switch to a new browser window by its handle in Selenium Python?
easy
A. driver.switch_to.alert(window_handle)
B. driver.switch_to.default_content(window_handle)
C. driver.switch_to.frame(window_handle)
D. driver.switch_to.window(window_handle)

Solution

  1. Step 1: Recall method to switch windows

    To switch to a new browser window, use driver.switch_to.window() with the window handle.
  2. Step 2: Confirm correct syntax

    The method requires the window handle string as an argument, so driver.switch_to.window(window_handle) is correct.
  3. Final Answer:

    driver.switch_to.window(window_handle) -> Option D
  4. Quick Check:

    Switch window syntax = driver.switch_to.window(handle) [OK]
Hint: Use switch_to.window(handle) to change browser tabs/windows [OK]
Common Mistakes:
  • Using switch_to.alert for windows
  • Passing window handle to switch_to.frame
  • Omitting the window handle argument
3. Given the following code snippet, what will be printed?
driver.get('https://example.com')
main_window = driver.current_window_handle
# Assume clicking button opens a new window
button = driver.find_element('id', 'openWindow')
button.click()
all_windows = driver.window_handles
for handle in all_windows:
    if handle != main_window:
        driver.switch_to.window(handle)
        print(driver.title)
        driver.close()
        break

# Switch back to main window
driver.switch_to.window(main_window)
print(driver.title)
medium
A. Title of new window followed by title of main window
B. Only title of main window printed twice
C. Only title of new window printed
D. No output due to error

Solution

  1. Step 1: Understand window switching and closing

    The code clicks a button that opens a new window, then switches to it, prints its title, closes it, and breaks the loop.
  2. Step 2: Switch back and print main window title

    After closing the new window, the code switches back to the main window and prints its title.
  3. Final Answer:

    Title of new window followed by title of main window -> Option A
  4. Quick Check:

    Print new window title, then main window title [OK]
Hint: Switch to new window, print title, close, then switch back [OK]
Common Mistakes:
  • Not switching back to main window before printing
  • Trying to print after closing main window
  • Assuming both titles print simultaneously
4. Identify the error in the following Selenium Python code for handling an alert pop-up:
alert = driver.switch_to.alert()
alert.accept()
medium
A. Incorrect use of parentheses after switch_to.alert
B. Missing import for Alert class
C. alert.accept() should be alert.dismiss()
D. No error, code is correct

Solution

  1. Step 1: Check method usage for alert switching

    driver.switch_to.alert is a property, not a method, so it should not have parentheses.
  2. Step 2: Correct the syntax

    Remove parentheses: use alert = driver.switch_to.alert to get the alert object.
  3. Final Answer:

    Incorrect use of parentheses after switch_to.alert -> Option A
  4. Quick Check:

    switch_to.alert is property, no () [OK]
Hint: Don't add () after switch_to.alert, it's a property [OK]
Common Mistakes:
  • Adding parentheses after switch_to.alert
  • Confusing accept() with dismiss()
  • Not switching to alert before accepting
5. You have a test that opens multiple pop-up windows in sequence. Which approach correctly handles switching to each new window, closing it, and returning to the main window without errors?
hard
A. Store main window handle, loop through all handles, switch to each new window, close it, then switch back to main window after loop
B. Switch to each window and close it inside the loop, then switch back to main window after closing all windows
C. Switch to each new window, close it immediately, and switch back to main window inside the loop after each close
D. Close main window first, then switch to new windows one by one

Solution

  1. Step 1: Save main window handle before opening pop-ups

    Store the main window handle to return to it later safely.
  2. Step 2: Loop through all window handles, switch, close, and return

    For each new window, switch to it, close it, then switch back to main window inside the loop to avoid stale window errors.
  3. Step 3: Avoid closing main window before others

    Closing the main window first causes errors; it should be closed last or not at all during pop-up handling.
  4. Final Answer:

    Switch to each new window, close it immediately, and switch back to main window inside the loop after each close -> Option C
  5. Quick Check:

    Switch-close-switch back per window to avoid errors [OK]
Hint: Switch-close-switch back inside loop for each pop-up [OK]
Common Mistakes:
  • Closing main window before others
  • Switching back only after closing all pop-ups
  • Not storing main window handle before loop
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard