Window handles help you switch between different browser windows or tabs during automated tests.
Window handles in Selenium Python
driver.window_handles # Returns a list of all open window handles driver.current_window_handle # Returns the handle of the current window driver.switch_to.window(window_handle) # Switches control to the specified window
Window handles are unique IDs for each open browser window or tab.
You must switch to the desired window before interacting with elements inside it.
handles = driver.window_handles
print(handles)main_window = driver.current_window_handle
print(main_window)driver.switch_to.window(handles[1])This script opens a webpage with a button that opens a new window. It clicks the button, switches to the new window, prints its title, closes it, then switches back to the main window and prints its title again.
from selenium import webdriver from selenium.webdriver.common.by import By import time # Setup driver driver = webdriver.Chrome() driver.get('https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_win_open') # Click button to open new window iframe = driver.find_element(By.ID, 'iframeResult') driver.switch_to.frame(iframe) driver.find_element(By.TAG_NAME, 'button').click() # Get window handles handles = driver.window_handles print(f"All window handles: {handles}") # Switch to new window main_window = driver.current_window_handle driver.switch_to.window(handles[1]) print(f"Switched to new window title: {driver.title}") # Close new window driver.close() # Switch back to main window driver.switch_to.window(main_window) print(f"Back to main window title: {driver.title}") driver.quit()
Window handles are strings and unique per browser session.
Always switch back to the main window after closing child windows to avoid errors.
Use explicit waits if the new window takes time to load before switching.
Window handles let you control multiple browser windows or tabs in Selenium.
You get all handles with driver.window_handles and switch with driver.switch_to.window().
Switching windows is essential to interact with elements in popups or new tabs.
Practice
driver.window_handles return in Selenium?Solution
Step 1: Understand the purpose of
This command returns all the unique IDs (handles) of open browser windows or tabs controlled by Selenium.driver.window_handlesStep 2: Differentiate from other window properties
It does not return titles, URLs, or sizes, only handles to switch between windows.Final Answer:
A list of all open browser window handles -> Option DQuick Check:
window_handles = list of handles [OK]
- Confusing window_handles with current window title
- Expecting a single handle instead of a list
- Using window_handles to get URL or size
Solution
Step 1: Recall Selenium Python syntax for switching windows
The correct method isdriver.switch_to.window(handle)to switch context to the window with the given handle.Step 2: Identify incorrect syntax options
Options B, C, and D use incorrect method names or Java-like syntax not valid in Python Selenium.Final Answer:
driver.switch_to.window(new_window_handle) -> Option BQuick Check:
Use switch_to.window() in Python Selenium [OK]
- Using Java syntax like switchTo() in Python
- Missing '_to' in switch_to
- Using non-existent methods like switch_window
original_handle = driver.current_window_handle
all_handles = driver.window_handles
for handle in all_handles:
if handle != original_handle:
driver.switch_to.window(handle)
print(driver.title)
breakSolution
Step 1: Understand the code logic
The code saves the original window handle, then loops through all handles. It switches to the first handle that is not the original.Step 2: Determine what is printed
After switching, it prints the title of the new window and breaks the loop, so it prints the new window's title.Final Answer:
The title of the new window (not original) -> Option CQuick Check:
Switch to non-original window, print title [OK]
- Expecting original window title to print
- Confusing current_window_handle with window_handles
- Thinking loop does not run
new_window = driver.window_handles[1] driver.switch_to.window(new_window) driver.close()
Solution
Step 1: Analyze window_handles indexing
Accessing index 1 assumes there are at least two windows open. If only one window exists, index 1 causes an IndexError.Step 2: Check other statements
switch_to.window() is valid, driver.close() closes only current window, so no issue there.Final Answer:
Index 1 may not exist if only one window is open -> Option AQuick Check:
Check window_handles length before indexing [OK]
- Assuming multiple windows always exist
- Thinking driver.close() closes all windows
- Confusing method names
Solution
Step 1: Understand the problem
You want to switch to the new tab but only know the current window handle, not the new one.Step 2: Analyze each option
new_handle = [h for h in driver.window_handles if h != driver.current_window_handle][0] driver.switch_to.window(new_handle) finds the handle that is not the current one and switches to it correctly.
driver.switch_to.window(driver.current_window_handle + 1) tries to add 1 to a string handle, causing error.
driver.switch_to.window(driver.window_handles[-1]) switches to the last handle, which may work but is unsafe if multiple windows exist.
driver.switch_to.window(driver.window_handles[0]) switches back to the original window, not the new tab.Final Answer:
Use list comprehension to find new handle and switch -> Option AQuick Check:
Find new handle by exclusion, then switch [OK]
- Adding 1 to a string handle (invalid)
- Assuming last handle is always new
- Switching to original window by mistake
Ready to prove it?
Pass the test and this pattern moves unknown
