Multiple Windows

Introduction

Modern web applications often open new browser windows when users click certain links or buttons. Examples include payment gateways, help pages, reports, document previews, and third-party authentication pages.

Selenium identifies every browser window or tab using a unique Window Handle. By switching between these window handles, Selenium can automate interactions across multiple browser windows.

In this tutorial, you’ll learn how to work with multiple browser windows using Selenium with Python, along with practical examples, real-world scenarios, common mistakes, and best practices.


What are Multiple Windows?

A Multiple Window scenario occurs when an action on a webpage opens another browser window.

For example:

Main Window

        │
        ▼

Click "Open"

        │
        ▼

New Browser Window

Each browser window has its own unique Window Handle.


What is a Window Handle?

A Window Handle is a unique identifier assigned by Selenium to every open browser window or tab.

Common properties include:

  • driver.current_window_handle

  • driver.window_handles

These properties help Selenium identify and switch between browser windows.


Why Automate Multiple Windows?

Automating multiple windows helps you:

  • Test payment gateways.

  • Verify external links.

  • Automate document previews.

  • Test authentication workflows.

  • Improve automation coverage.


Example

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait


# Topic: 30. Windows and Tabs - Multiple Windows
# Practice site: https://the-internet.herokuapp.com/windows
# Run: pytest -s 30_examples/test_01_multiple_windows.py
#
# Selenium stores every open browser window or tab as a window handle. After a
# click opens a new window, wait until there are two handles.


def test_open_multiple_windows():
    driver = webdriver.Chrome()

    try:
        driver.get("https://the-internet.herokuapp.com/windows")

        original_window = driver.current_window_handle
        driver.find_element(By.LINK_TEXT, "Click Here").click()

        WebDriverWait(driver, 10).until(
            lambda browser: len(browser.window_handles) == 2
        )

        assert original_window in driver.window_handles
        assert len(driver.window_handles) == 2
    finally:
        driver.quit()

Understanding the Code

Import Required Libraries

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

These modules are required to launch the browser, locate web elements, and wait until the second browser window opens.


Create a Chrome Browser Instance

driver = webdriver.Chrome()

Starts a new Chrome browser session.


Open the Practice Website

driver.get("https://the-internet.herokuapp.com/windows")

Navigates to the Multiple Windows practice page.


Store the Original Window

original_window = driver.current_window_handle

Retrieves and stores the unique handle of the currently active browser window.

This allows Selenium to identify the original window later.


Open a New Window

driver.find_element(
    By.LINK_TEXT,
    "Click Here"
).click()

Clicks the Click Here link, which opens a new browser window.


Wait Until Two Windows Exist

WebDriverWait(driver, 10).until(
    lambda browser: len(browser.window_handles) == 2
)

Waits until Selenium detects two browser window handles.

This ensures the new window has opened before continuing.


Verify the Original Window Still Exists

assert original_window in driver.window_handles

Confirms that the original browser window is still available.


Verify Two Windows Are Open

assert len(driver.window_handles) == 2

Checks that exactly two browser windows are currently open.

If not, the test fails.


Close the Browser

driver.quit()

Closes all browser windows and ends the WebDriver session.


Practical Example

Suppose an e-commerce website opens the payment gateway in a new browser window.

The automation script:

  • Opens the checkout page.

  • Clicks Proceed to Payment.

  • Waits for the payment window to open.

  • Verifies that two browser windows are available.


Automation Testing Example

Consider an HR portal.

Clicking View Employee Handbook opens the handbook in a new browser window.

The automation script:

  • Opens the employee portal.

  • Clicks the handbook link.

  • Waits for the second window.

  • Verifies that both windows are available before switching.


Real-World Example

Multiple browser windows are commonly used in:

  • Banking applications

  • Payment gateways

  • Government portals

  • HR management systems

  • Healthcare applications

  • E-commerce websites

  • Enterprise web applications

Examples include payment pages, document previews, reports, help pages, and external authentication systems.


Advantages of Automating Multiple Windows

  • Supports complex user workflows.

  • Tests external integrations.

  • Verifies browser navigation.

  • Improves automation coverage.

  • Simulates real user behavior.


Common Mistakes Beginners Make

Assuming the New Window Opens Immediately

Always wait until the expected number of window handles is available.


Forgetting to Store the Original Window

Save the original window handle before opening another window.

This makes it easy to return to the original page later.


Assuming There Will Always Be Two Windows

Verify the number of window handles before switching to another window.


Closing the Browser Too Early

Ensure all required validations are completed before calling driver.quit().


Best Practices

  • Store the original window handle before opening a new window.

  • Use Explicit Wait to wait for new windows.

  • Verify the number of window handles before switching.

  • Use driver.window_handles instead of hardcoded assumptions.

  • Close browser sessions properly after the test.


Conclusion

Multiple browser windows are commonly used in modern web applications for payment gateways, external links, reports, and authentication workflows. Selenium manages these windows using unique window handles, allowing automation scripts to detect, verify, and switch between browser windows reliably. Understanding window handles is an essential skill for building robust Selenium automation frameworks.


Frequently Asked Questions (FAQs)

What is a window handle?

A window handle is a unique identifier assigned by Selenium to every open browser window or tab.


How do I get the current window handle?

Use:

driver.current_window_handle

How do I get all open window handles?

Use:

driver.window_handles

Why should I wait before checking window handles?

Because opening a new browser window may take a short time. Using an Explicit Wait ensures Selenium detects the new window before continuing.


Where are multiple browser windows commonly used?

They are commonly used in payment gateways, document previews, authentication pages, help pages, enterprise applications, and external website integrations.


Key Takeaways

  • Every browser window has a unique window handle.

  • Use driver.current_window_handle to identify the current window.

  • Use driver.window_handles to retrieve all open windows.

  • Use an Explicit Wait to detect newly opened windows.

  • Store the original window handle before opening another window.

  • Window handling is an essential Selenium skill for automating modern web applications.