Handling Alerts, Frames & Windows
Browser alerts, iframes, and multiple browser windows/tabs require special Selenium handling that trips up most beginners. These features appear frequently in legacy systems, payment flows, and modal dialogs — and failing to handle them correctly causes tests to hang or miss critical validation steps.
Alerts, Frames, and Multi-Window Handling
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# ══════════════════════════════════════════════════════════════
# BROWSER ALERTS (JavaScript alert, confirm, prompt)
# ══════════════════════════════════════════════════════════════
# Trigger delete action that shows: alert("Are you sure?")
delete_btn = driver.find_element(By.ID, "delete-account")
delete_btn.click()
# Switch to the alert
wait = WebDriverWait(driver, 10)
alert = wait.until(EC.alert_is_present())
# Read alert text
print(f"Alert text: {alert.text}") # "Are you sure?"
# Accept (click OK) or dismiss (click Cancel)
alert.accept() # Click OK — confirms the action
# alert.dismiss() # Click Cancel — cancels the action
# For prompt() — send text before accepting
alert.send_keys("My input text")
alert.accept()
# ══════════════════════════════════════════════════════════════
# IFRAMES (embedded frames within the page)
# ══════════════════════════════════════════════════════════════
# Three ways to switch into an iframe:
driver.switch_to.frame("frameName") # By name attribute
driver.switch_to.frame(0) # By index (0 = first)
iframe_element = driver.find_element(By.ID, "payment-iframe")
driver.switch_to.frame(iframe_element) # By WebElement
# Now interact with elements inside the iframe
card_number = driver.find_element(By.ID, "card-number")
card_number.send_keys("4111111111111111")
# IMPORTANT: Return to main page content after iframe interaction
driver.switch_to.default_content()
# Navigate to nested iframes:
driver.switch_to.frame("outer-frame")
driver.switch_to.frame("inner-frame") # Switch to frame within frame
driver.switch_to.parent_frame() # Go up one frame level
driver.switch_to.default_content() # Return to main page
# ══════════════════════════════════════════════════════════════
# MULTIPLE WINDOWS/TABS
# ══════════════════════════════════════════════════════════════
# Capture current window handle before opening new one
main_window = driver.current_window_handle
# Click link that opens new tab/window
driver.find_element(By.LINK_TEXT, "Open Preview").click()
# Wait for new window and switch to it
wait.until(EC.number_of_windows_to_be(2))
all_handles = driver.window_handles
# Switch to the new window
new_window = [h for h in all_handles if h != main_window][0]
driver.switch_to.window(new_window)
# Interact with new window
assert "Preview" in driver.title
driver.close() # Close NEW window
# Return to main window
driver.switch_to.window(main_window)Common Mistakes
- Interacting with elements before switching to iframe — elements inside iframes are invisible from the main page context; always switch first
- Forgetting to switch back after iframe — subsequent find_element calls will still search inside the iframe; call switch_to.default_content()
- Not waiting for alerts — clicking a button that triggers an alert requires waiting for EC.alert_is_present() before calling alert.accept()
- Losing window handles — always capture all window handles before and after opening new windows; don't assume indices
Tip
Tip
Practice Handling Alerts Frames Windows in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Testing pyramid: many unit tests, fewer integration, fewest E2E
Practice Task
Note
Practice Task — (1) Write a working example of Handling Alerts Frames Windows from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Handling Alerts Frames Windows is skipping edge case testing — empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready software testing code.
Key Takeaways
- Browser alerts, iframes, and multiple browser windows/tabs require special Selenium handling that trips up most beginners.
- Interacting with elements before switching to iframe — elements inside iframes are invisible from the main page context; always switch first
- Forgetting to switch back after iframe — subsequent find_element calls will still search inside the iframe; call switch_to.default_content()
- Not waiting for alerts — clicking a button that triggers an alert requires waiting for EC.alert_is_present() before calling alert.accept()