Handling Multiple Windows in Selenium WebDriver Java
In real-world web applications, users often encounter scenarios where clicking a link or button opens a new browser window or tab. This might be for displaying additional information, login forms, or third-party integrations. When automating such workflows using Selenium WebDriver with Java, it is essential to understand how to handle multiple windows effectively.
In this blog, we’ll explore the techniques for switching between windows, capturing window handles, and closing specific windows using Selenium WebDriver in Java.
π§ Understanding Window Handles
Each window or tab opened in a browser has a unique identifier known as a window handle. Selenium uses these handles to switch between different windows during the test execution.
java
String parentWindow = driver.getWindowHandle();
Set<String> allWindows = driver.getWindowHandles();
getWindowHandle() – returns the handle of the current window.
getWindowHandles() – returns a Set<String> of all open window handles.
π Switching Between Windows
To perform actions in the newly opened window, we must switch the WebDriver’s context to it:
java
for (String windowHandle : driver.getWindowHandles()) {
if (!windowHandle.equals(parentWindow)) {
driver.switchTo().window(windowHandle);
break;
}
}
After switching, you can perform actions like clicking elements, extracting text, or validating content.
✅ Example Use Case
Here’s a complete example of handling multiple windows:
java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.Set;
public class MultipleWindowHandling {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
// Click to open a new window
driver.findElement(By.id("openWindow")).click();
// Store the main window handle
String mainWindow = driver.getWindowHandle();
// Get all window handles
Set<String> allWindows = driver.getWindowHandles();
// Switch to the new window
for (String window : allWindows) {
if (!window.equals(mainWindow)) {
driver.switchTo().window(window);
System.out.println("Switched to new window: " + driver.getTitle());
// Perform operations on the new window
driver.findElement(By.id("submitButton")).click();
// Close the new window
driver.close();
}
}
// Switch back to the main window
driver.switchTo().window(mainWindow);
System.out.println("Back to main window: " + driver.getTitle());
driver.quit();
}
}
π ️ Tips for Working with Multiple Windows
Always store the parent window handle before opening a new window.
Avoid hardcoding waits. Use explicit waits to ensure the new window is fully loaded.
Use proper conditions if you expect more than two windows (e.g., file downloads, popups).
Always switch back to the original window after performing the necessary operations.
Close the new window if it’s no longer needed, to avoid memory leaks in longer test runs.
⚠️ Common Pitfalls
Forgetting to switch context leads to NoSuchElementException.
Assuming window titles are unique—always validate using windowHandle.
Not closing child windows may cause test failure in long-running sessions.
π― Conclusion
Handling multiple windows is a fundamental skill in Selenium WebDriver automation with Java. Whether it’s for dealing with popups, login modals, or new tabs, mastering window handles allows you to build robust and realistic automation scripts. By following best practices for switching and managing window contexts, you can ensure your tests remain reliable and maintainable across different use cases.
Learn Selenium with Java Training
Read More: Using Assertions in Selenium Java with TestNG
Read More: Automating Login Functionality Using Selenium Java
Read More: Automating Login Functionality Using Selenium Java
Visit Our IHUB Talent Institute Hyderabad
Get Direction
Comments
Post a Comment