5 Best Ways to Handle Child Windows in Selenium with Python

πŸ’‘ Problem Formulation: When automating web browsers with Selenium using Python, developers often need to interact with multiple windows. This scenario occurs, for instance, when a user action opens a new window, and we wish to automate actions in it before possibly returning to the main window. The challenge lies in seamlessly switching between the main window (the parent) and the newly opened windows (the children). This article details methods to identify and switch control to these child windows, enabling the desired automation workflows.

Method 1: Switching to the Child Window Using Window Handles

Every window generated by a WebDriver session has a unique identifier called the window handle. This method involves capturing all window handles and switching to the window handle that’s not the parent window. This works well when you have one or a manageable number of child windows.

Here’s an example:

from selenium import webdriver

# Start the driver and open a URL
driver = webdriver.Chrome()
driver.get("http://example.com")

# Store the ID of the original window
original_window = driver.current_window_handle

# Perform some action that opens a new window
driver.find_element_by_id("new_window").click()

# Switch to the new window
for window_handle in driver.window_handles:
    if window_handle != original_window:
        driver.switch_to.window(window_handle)
        break

The output is the WebDriver’s context switched to the new window, where further actions can be automated.

This code snippet starts by opening a page in Chrome with Selenium’s WebDriver. After the user performs an action that opens a new window, it captures the handles for all the windows and iterates through them. It then switches to the first window that isn’t the original window, assuming it’s the child window.

Method 2: Closing the Child Window and Switching Back to the Parent Window

Once done with the tasks in the child window, it may be necessary to close it and return to the parent window. This method includes closing the child window and ensuring the WebDriver’s focus is back on the parent window, where automation can continue.

Here’s an example:

from selenium import webdriver

# Start the driver and open a URL
driver = webdriver.Chrome()
driver.get("http://example.com")

# Perform some action that opens a new window
# Store the ID of the original window
original_window = driver.current_window_handle

# ... (Work in the new window)

# Close the child window
driver.close()

# Switch back to the original window
driver.switch_to.window(original_window)

The output is the WebDriver’s context switched back to the original window after closing the child window.

After completing the necessary actions in the new window, the code snippet closes the child window using driver.close(), and then switches focus back to the original window using driver.switch_to.window() with the original window’s handle.

Method 3: Opening and Directly Switching to a New Tab or Window

Using JavaScript, you can open a new tab or window and gain immediate control over it. This can be particularly useful when you need to preload some settings or navigate to a specific URL directly in a new window or tab.

Here’s an example:

from selenium import webdriver

# Start the driver
driver = webdriver.Chrome()

# Use JavaScript to open a new window
driver.execute_script("window.open('http://example.com');")

# Switch to the new window
driver.switch_to.window(driver.window_handles[-1])

The output is a new browser tab or window opened to ‘http://example.com’, with the WebDriver’s context switched to it immediately.

Here, execute_script() is used to run a JavaScript command that opens a new window with a specified URL. The window_handles attribute of the WebDriver is then used to switch to this new window, with the assumption that it’s the last opened window.

Method 4: Waiting for a Specific Child Window to Appear and Switching to It

In some cases, the child window might take a while to load or appear. It is important to wait for the window to be present before switching to it. This method combines explicit waits with the switching mechanism to ensure reliable automation.

Here’s an example:

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

# Start the driver
driver = webdriver.Chrome()

# Open the original window:
driver.get('http://example.com')

# Perform action that opens new window
new_window_button = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.ID, "open-new-window"))
)
new_window_button.click()

# Wait for the new window or tab
WebDriverWait(driver, 10).until(
    EC.number_of_windows_to_be(2)
)

# Loop through until we find a new window handle
for window_handle in driver.window_handles:
    if window_handle != original_window:
        driver.switch_to.window(window_handle)
        break

The output is the WebDriver’s context switched to the newly appeared window, after ensuring it exists and is ready for interaction.

This snippet waits for a button that opens a new window to be clickable using WebDriverWait and expected_conditions. Once the new window is opened and detected (since the number of windows has changed), it switches to it as soon as it’s ready.

Bonus One-Liner Method 5: Switching to a New Window with List Comprehension

For Python enthusiasts who love one-liners, this method offers a compact way of switching to the child window using list comprehension.

Here’s an example:

from selenium import webdriver

# Start the driver
driver = webdriver.Chrome()

# Perform action that opens new window
# ...

# Switch to new window using a one-liner
driver.switch_to.window([handle for handle in driver.window_handles if handle != driver.current_window_handle][0])

The output is a swift transition to the child window using Python’s list comprehension feature.

This succinct snippet uses list comprehension to filter out the current window handle from the list of all window handles and immediately switches to the new window, assuming there’s only one additional window open.

Summary/Discussion

  • Method 1: Switching to the Child Window Using Window Handles. Simple and effective for most use cases. Can get cumbersome if there are multiple new windows to manage.
  • Method 2: Closing the Child Window and Switching Back to the Parent Window. Essential for clean-up actions in automation scripts. Requires proper handling to ensure no memory leaks or hanging windows.
  • Method 3: Opening and Directly Switching to a New Tab or Window. Convenient for direct navigation. Less control over how and when the new window appears.
  • Method 4: Waiting for a Specific Child Window to Appear and Switching to It. Ensures robustness in scripts by waiting for windows to load. Can slow down script execution based on the set timeout.
  • Method 5: Bonus One-Liner. Concise and Pythonic. Assumes only one child window and may require modification for more complex scenarios.