Selenium Cheatsheet
Frames and Windows
Use this Selenium reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.
iFrames
Switching Into a Frame
from selenium.webdriver.common.by import By # By index (0-based) driver.switch_to.frame(0) # By name or id attribute driver.switch_to.frame("iframe-name") driver.switch_to.frame("iframe-id") # By WebElement iframe_el = driver.find_element(By.CSS_SELECTOR, "iframe.content-frame") driver.switch_to.frame(iframe_el)
Switching Back Out
driver.switch_to.parent_frame() # go up one level (nested frames) driver.switch_to.default_content() # return to top-level document
Wait Before Switching
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC wait = WebDriverWait(driver, 10) wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe.editor"))) # Now interact inside the frame editor = driver.find_element(By.ID, "content-area") editor.send_keys("Hello from inside the frame")
Nested Frames
# Switch into outer frame, then inner frame driver.switch_to.frame("outer-frame") driver.switch_to.frame("inner-frame") # Do work... driver.find_element(By.ID, "inner-element").click() # Exit to parent frame first, then to default driver.switch_to.parent_frame() driver.switch_to.default_content()
Full iFrame Pattern
# 1. Locate and switch iframe = driver.find_element(By.TAG_NAME, "iframe") driver.switch_to.frame(iframe) # 2. Interact inside the frame driver.find_element(By.ID, "submit").click() # 3. Always return to the main document when done driver.switch_to.default_content()
Windows and Tabs
Opening and Tracking Windows
# Capture the original window handle original_window = driver.current_window_handle # All current handles print(driver.window_handles) # list of handle strings # Open a new tab (Selenium 4+) driver.switch_to.new_window("tab") # Open a new browser window (Selenium 4+) driver.switch_to.new_window("window")
Switching Between Windows
# Switch by handle for handle in driver.window_handles: driver.switch_to.window(handle) if "expected title" in driver.title: break # Switch to a newly opened window (the one that isn't the original) wait = WebDriverWait(driver, 10) wait.until(EC.number_of_windows_to_be(2)) for handle in driver.window_handles: if handle != original_window: driver.switch_to.window(handle) break
Closing a Window / Tab
# Close the current tab driver.close() # Switch back to the original window after closing driver.switch_to.window(original_window)
Full Multi-Window Pattern
original = driver.current_window_handle # Trigger action that opens a new tab/window driver.find_element(By.LINK_TEXT, "Open Preview").click() # Wait for the new window and switch to it WebDriverWait(driver, 10).until(EC.number_of_windows_to_be(2)) new_window = [h for h in driver.window_handles if h != original][0] driver.switch_to.window(new_window) # Do work in the new window print(driver.title) # Close the new window and return driver.close() driver.switch_to.window(original)
Shadow DOM
# Selenium 4.1+ supports shadow roots natively host = driver.find_element(By.CSS_SELECTOR, "my-custom-element") shadow_root = host.shadow_root # Find elements inside the shadow root inner_el = shadow_root.find_element(By.CSS_SELECTOR, "#inner-input") inner_el.send_keys("hello")
Shadow DOM via JavaScript (fallback)
host = driver.find_element(By.CSS_SELECTOR, "my-custom-element") shadow_root = driver.execute_script("return arguments[0].shadowRoot;", host) inner = shadow_root.find_element(By.CSS_SELECTOR, "#inner-btn") inner.click()
Popup Windows (Browser-Native)
# Browser popups triggered by window.open() are treated as new windows original = driver.current_window_handle driver.find_element(By.ID, "open-popup").click() WebDriverWait(driver, 10).until(EC.number_of_windows_to_be(2)) popup = [h for h in driver.window_handles if h != original][0] driver.switch_to.window(popup) # Interact with popup driver.find_element(By.ID, "popup-confirm").click() driver.close() driver.switch_to.window(original)
Gotchas
Always call
driver.switch_to.default_content()after finishing frame interactions — subsequentfind_elementcalls search only the currently active frame context.
frame_to_be_available_and_switch_to_itboth waits and switches in one call — no separateswitch_to.frame()needed after it.
driver.close()without switching back leaves the driver pointed at a closed window, causingNoSuchWindowExceptionon the next command.
Shadow DOM
find_elementsdoes not support XPath — only CSS selectors work inside a shadow root.