Handling pop-up windows in Selenium Python - Build an Automation Script
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 # Initialize the Chrome driver with webdriver.Chrome() as driver: driver.get('https://example.com/mainpage') # Replace with actual URL main_window = driver.current_window_handle # Click the button to open the popup open_popup_button = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.ID, 'open-popup')) ) open_popup_button.click() # Wait for new window to appear WebDriverWait(driver, 10).until( lambda d: len(d.window_handles) > 1 ) # Switch to the popup window popup_window = [w for w in driver.window_handles if w != main_window][0] driver.switch_to.window(popup_window) # Assert popup window title WebDriverWait(driver, 10).until(EC.title_is('Popup Window')) assert driver.title == 'Popup Window', f"Expected popup title 'Popup Window' but got '{driver.title}'" # Assert popup contains expected paragraph text popup_paragraph = WebDriverWait(driver, 10).until( EC.visibility_of_element_located((By.CSS_SELECTOR, 'p.popup-text')) ) assert popup_paragraph.text == 'Welcome to the popup!', f"Expected popup text 'Welcome to the popup!' but got '{popup_paragraph.text}'" # Close the popup window driver.close() # Switch back to main window driver.switch_to.window(main_window) # Assert main window title WebDriverWait(driver, 10).until(EC.title_is('Main Page')) assert driver.title == 'Main Page', f"Expected main page title 'Main Page' but got '{driver.title}'"
This script starts by opening the main page URL and saving the main window handle.
It waits until the 'Open Popup' button is clickable, then clicks it to open the pop-up window.
It waits until a new window handle appears, then switches control to the new pop-up window.
It asserts the pop-up window's title and verifies the paragraph text inside it.
After verification, it closes the pop-up window and switches back to the main window.
Finally, it asserts the main window's title to confirm control returned correctly.
Explicit waits ensure the script waits for elements and windows properly without using fixed delays.
Using descriptive variable names and proper window handle management makes the code clear and maintainable.
Now add data-driven testing with 3 different pop-up button IDs and expected titles/texts
Practice
Solution
Step 1: Understand alert handling in Selenium
Alerts are special pop-ups that require switching to alert context usingdriver.switch_to.alert.Step 2: Identify the correct method to switch
Usingdriver.switch_to.alertaccesses the alert object directly, allowing interaction.Final Answer:
driver.switch_to.alert -> Option BQuick Check:
Alert pop-up switch = driver.switch_to.alert [OK]
- Using switch_to.window for alerts
- Trying to switch to alert as a frame
- Not switching before interacting with alert
Solution
Step 1: Recall method to switch windows
To switch to a new browser window, usedriver.switch_to.window()with the window handle.Step 2: Confirm correct syntax
The method requires the window handle string as an argument, sodriver.switch_to.window(window_handle)is correct.Final Answer:
driver.switch_to.window(window_handle) -> Option DQuick Check:
Switch window syntax = driver.switch_to.window(handle) [OK]
- Using switch_to.alert for windows
- Passing window handle to switch_to.frame
- Omitting the window handle argument
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)Solution
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.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.Final Answer:
Title of new window followed by title of main window -> Option AQuick Check:
Print new window title, then main window title [OK]
- Not switching back to main window before printing
- Trying to print after closing main window
- Assuming both titles print simultaneously
alert = driver.switch_to.alert() alert.accept()
Solution
Step 1: Check method usage for alert switching
driver.switch_to.alertis a property, not a method, so it should not have parentheses.Step 2: Correct the syntax
Remove parentheses: usealert = driver.switch_to.alertto get the alert object.Final Answer:
Incorrect use of parentheses after switch_to.alert -> Option AQuick Check:
switch_to.alert is property, no () [OK]
- Adding parentheses after switch_to.alert
- Confusing accept() with dismiss()
- Not switching to alert before accepting
Solution
Step 1: Save main window handle before opening pop-ups
Store the main window handle to return to it later safely.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.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.Final Answer:
Switch to each new window, close it immediately, and switch back to main window inside the loop after each close -> Option CQuick Check:
Switch-close-switch back per window to avoid errors [OK]
- Closing main window before others
- Switching back only after closing all pop-ups
- Not storing main window handle before loop
Ready to prove it?
Pass the test and this pattern moves unknown
