The getWindowHandles() method in Selenium WebDriver is a crucial tool for managing and interacting with multiple browser windows or tabs in an automated test session. This method is commonly used in scenarios where your script needs to switch between different browser contexts—such as when a link opens a new tab or popup window.
What Does getWindowHandles() Do?
- Returns: A
Set<String>
where each element is a unique identifier (handle) for an open window or tab in the current browser session. - Difference from
getWindowHandle()
: WhilegetWindowHandle()
returns the handle of the current (single) window,getWindowHandles()
returns all handles, allowing iteration and control over every open window.
Use Case: Handling Multiple Windows
Typical multi-window workflows involve:
- Opening one or more new windows or tabs from your main (parent) window.
- Retrieving all window handles using
getWindowHandles()
. - Iterating through these handles and switching context with
driver.switchTo().window(handle)
to perform actions in each window.
Example: Switch Between Windows
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import java.util.Set; public class HandleMultipleWindows { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); WebDriver driver = new ChromeDriver(); // Open parent window driver.get("http://www.example.com/"); String parentHandle = driver.getWindowHandle(); // Store parent window handle // Assume clicking this link opens new windows driver.findElement(By.linkText("Open New Window")).click(); // Get all window handles Set<String> allHandles = driver.getWindowHandles(); // Iterate through all handles for (String handle : allHandles) { driver.switchTo().window(handle); System.out.println("Title: " + driver.getTitle()); // Perform desired actions in the window } // Return to parent window driver.switchTo().window(parentHandle); driver.quit(); } }
This example demonstrates:
- How to get all currently open windows.
- How to loop through them and switch context.
Key Points
- Returns a Set: The order of window handles in the set is not guaranteed; it simply lists all open windows at the time of method call.
- Switch with Care: Selenium does not automatically switch to new windows—you must use
switchTo().window(handle)
. - Practical Usage: Useful for closing popups, handling third-party authentication flows, or any workflow involving window/tab switching.
- Iterate Safely: Always store your main window handle before switching, so you can return to it after operations in other windows.
Summary Table
Method | Return Type | What It Returns | Use For |
---|---|---|---|
getWindowHandle() | String | Handle of current window/tab | Tracking main/parent window |
getWindowHandles() | Set<String> | Handles for all open windows/tabs | Iterating and switching windows |
The getWindowHandles()
method is fundamental when creating robust Selenium automation that must control more than one browser window or tab. By leveraging it with switchTo().window(handle)
, you can seamlessly automate complex workflows spanning multiple browser contexts.