Pop-up windows often appear during web testing. Handling them lets you control or close these windows to keep tests running smoothly.
0
0
Handling pop-up windows in Selenium Python
Introduction
When a website shows a login pop-up window.
When a confirmation alert appears after clicking a button.
When a new browser tab or window opens after a link click.
When testing a form that triggers a pop-up message.
When you need to switch focus between main page and pop-up.
Syntax
Selenium Python
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 # Switch to alert pop-up alert = driver.switch_to.alert # Accept the alert alert.accept() # Dismiss the alert alert.dismiss() # Get alert text text = alert.text # Switch to new window main_window = driver.current_window_handle all_windows = driver.window_handles for window in all_windows: if window != main_window: driver.switch_to.window(window) break
Use driver.switch_to.alert for alert pop-ups like alerts, confirms, prompts.
Use driver.switch_to.window(window_handle) to switch between browser windows or tabs.
Examples
This accepts (clicks OK) on a simple alert pop-up.
Selenium Python
alert = driver.switch_to.alert alert.accept()
This dismisses (clicks Cancel) on a confirmation pop-up.
Selenium Python
alert = driver.switch_to.alert alert.dismiss()
This switches control to a newly opened browser window or tab.
Selenium Python
main_window = driver.current_window_handle for window in driver.window_handles: if window != main_window: driver.switch_to.window(window) break
Sample Program
This script opens a test page with JavaScript alerts, clicks a button to open an alert, reads the alert text, accepts it, and prints the alert message.
Selenium Python
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.service import Service from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import time # Setup driver (make sure chromedriver is in PATH) service = Service() driver = webdriver.Chrome(service=service) try: driver.get('https://the-internet.herokuapp.com/javascript_alerts') # Click button to trigger alert driver.find_element(By.XPATH, '//button[text()="Click for JS Alert"]').click() # Wait for alert and accept it WebDriverWait(driver, 5).until(EC.alert_is_present()) alert = driver.switch_to.alert alert_text = alert.text alert.accept() print(f"Alert text was: {alert_text}") finally: time.sleep(2) # Pause to see result driver.quit()
OutputSuccess
Important Notes
Always wait for the alert to appear before switching to it to avoid errors.
After handling a pop-up, switch back to the main window if needed.
Pop-ups can be alerts, confirms, prompts, or new browser windows/tabs.
Summary
Pop-up windows need special handling to interact with them during tests.
Use driver.switch_to.alert for alerts and driver.switch_to.window() for new windows.
Always wait for pop-ups before interacting to avoid test failures.