Introduction
Modern web applications often open new browser windows or tabs when users click certain links or buttons. Examples include payment gateways, social media logins, help pages, terms and conditions, and external websites.
Selenium WebDriver provides built-in support for handling multiple browser windows using window handles. Every browser window opened during a Selenium session is assigned a unique identifier called a Window Handle. Selenium uses these handles to identify, switch between, and control different browser windows.
In this tutorial, you will learn how Selenium opens multiple browser windows, how window handles work, practical examples, real-world use cases, common mistakes, best practices, and frequently asked interview questions.
What are Multiple Browser Windows?
Multiple browser windows refer to two or more browser windows that are opened during the same Selenium WebDriver session.
For example:
Main Application Window
Login Popup
Payment Gateway Window
Help Documentation Window
Each browser window has its own unique Window Handle.
Why Do We Open Multiple Browser Windows?
Opening multiple browser windows is commonly required to:
Test login popups.
Validate payment gateways.
Verify external website links.
Test social media authentication.
Handle documentation links.
Perform multi-window workflows.
How Selenium Identifies Browser Windows
Selenium assigns a unique Window Handle to every browser window.
You can retrieve all open browser windows using:
driver.window_handles
The currently active browser window can be obtained using:
driver.current_window_handle
Practical Example
The following example demonstrates how Selenium opens multiple browser windows.
The automation script opens the Selenium practice website, clicks a link that opens a new browser window, waits until the second window appears, and verifies that two browser windows are open.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
# Topic: 7. Browser Window Management - Opening Multiple Browser Windows
# Practice site: https://the-internet.herokuapp.com/windows
#
# A link with target="_blank" opens a new browser window. Selenium tracks each
# window with a unique handle.
def test_open_multiple_browser_windows():
driver = webdriver.Chrome()
try:
driver.get("https://the-internet.herokuapp.com/windows")
driver.find_element(By.LINK_TEXT, "Click Here").click()
WebDriverWait(driver, 10).until(
lambda browser: len(browser.window_handles) == 2
)
assert len(driver.window_handles) == 2
finally:
driver.quit()
Output
Browser launched successfully.
Website opened successfully.
Clicked on "Click Here".
New browser window opened.
Total Browser Windows: 2
Assertion Passed.
Test Executed Successfully.
Understanding the Code
Import Required Modules
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
Imports Selenium WebDriver and the required classes.
Launch Chrome Browser
driver = webdriver.Chrome()
Starts a new Chrome browser session.
Open the Website
driver.get("https://the-internet.herokuapp.com/windows")
Navigates to the Selenium Multiple Windows practice page.
Click the Link
driver.find_element(By.LINK_TEXT, "Click Here").click()
Clicks the Click Here link, which opens a new browser window.
Wait Until Two Windows Are Open
WebDriverWait(driver, 10).until(
lambda browser: len(browser.window_handles) == 2
)
Waits until Selenium detects two browser windows.
Verify the Number of Windows
assert len(driver.window_handles) == 2
Checks that exactly two browser windows are open.
Close the Browser
driver.quit()
Closes all browser windows and ends the WebDriver session.
Browser Execution Flow
Launch Browser
│
▼
Open Website
│
▼
Click "Click Here"
│
▼
New Browser Window Opens
│
▼
Wait Until Two Windows Exist
│
▼
Verify Total Windows
│
▼
Assertions Pass
│
▼
Close Browser
Automation Testing Example
Suppose you’re testing an e-commerce application.
Clicking the Terms and Conditions link opens a new browser window.
Your automation script should:
Open the application.
Click the Terms and Conditions link.
Verify that a second browser window opens.
Continue testing.
Real-World Example
Consider an online banking application.
When a user clicks Net Banking, the payment gateway opens in a new browser window.
Selenium can verify that the payment window opens successfully before switching to it for further automation.
Common Mistakes Beginners Make
Forgetting to Wait for the New Window
Immediately checking the number of windows may fail if the new window hasn’t opened yet.
Incorrect
driver.find_element(By.LINK_TEXT, "Click Here").click()
assert len(driver.window_handles) == 2
Correct
WebDriverWait(driver, 10).until(
lambda browser: len(browser.window_handles) == 2
)
Confusing Windows with Tabs
Selenium treats browser tabs and browser windows in the same way.
Both are managed using Window Handles.
Assuming Only One Window Exists
Always check the number of open windows before switching.
len(driver.window_handles)
Best Practices
Use explicit waits before verifying newly opened windows.
Verify the number of browser windows before switching.
Store the parent window handle whenever required.
Close unnecessary browser windows after testing.
Always end the test using
driver.quit().
Conclusion
Opening multiple browser windows is a common requirement in Selenium automation. Selenium identifies every browser window using a unique Window Handle, making it easy to manage multiple windows during test execution.
Understanding how browser windows are created and detected is the foundation for learning advanced topics such as switching between windows, handling multiple tabs, and automating payment gateways or authentication popups.
Frequently Asked Questions (FAQs)
What are multiple browser windows in Selenium?
They are two or more browser windows opened during the same WebDriver session.
How does Selenium identify browser windows?
Using unique Window Handles.
Which property returns all browser windows?
driver.window_handles
How do I get the current browser window?
Use:
driver.current_window_handle
Why should I use an explicit wait when opening a new window?
Because the new browser window may take a few moments to appear, and waiting ensures Selenium detects it before continuing.
Key Takeaways
Selenium can automate multiple browser windows within a single WebDriver session.
Every browser window has a unique Window Handle.
driver.window_handlesreturns all open browser windows.driver.current_window_handlereturns the currently active browser window.Use explicit waits before verifying newly opened windows.
Browser tabs and browser windows are handled in the same way by Selenium.
Multiple browser windows are commonly used in payment gateways, social login, and external link testing.
Handling multiple browser windows is a fundamental Selenium concept and a frequently asked interview topic.
