Handling Multiple Windows in Selenium WebDriver
π Table of Contents
- πΉ What is Multiple Window Handling?
- πΉ Why Do We Need to Handle Multiple Windows?
- πΉ Key Methods for Handling Multiple Windows
- πΉ Switching to New Window – Java Code Example
- πΉ Handling Multiple Windows Using Iterator
- πΉ Handling Multiple Tabs in Selenium
- πΉ Switching Windows Using Titles
- πΉ Authentication Popups
- πΉ Key Takeaways
- ❓ FAQs
πΉ What is Multiple Window Handling in Selenium?
When you click on a link or button, a new window or tab may open. Selenium WebDriver by default stays on the main window and does not auto-switch.
✔ Key Selenium Methods:
getWindowHandle()
getWindowHandles()
switchTo().window()
close()
quit()
πΉ Why Do We Need to Handle Multiple Windows?
Web applications often open a new window for login, payments, popups, etc. Selenium must switch manually; otherwise, it will throw:
org.openqa.selenium.NoSuchElementException: Unable to locate element
π‘ Common Use Cases
- Login pop-ups (Google, Facebook auth)
- Payment gateways
- Product detail pages
- File upload/download popups
- Advertisement pop-ups
πΉ How Selenium Identifies & Switches Windows
π Method Table
Method | Description |
---|---|
getWindowHandle() | Returns current window ID |
getWindowHandles() | Returns Set of all open window IDs |
switchTo().window() | Switches focus to the specified window |
close() | Closes the current window |
quit() | Closes all windows |
πΉ Java Code to Handle New Window
✅ Real-Time Example: Click → New Window → Perform Action → Close → Switch Back
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.Set;
public class HandleMultipleWindows {
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
String parentWindow = driver.getWindowHandle();
WebElement link = driver.findElement(By.xpath("//a[@target='_blank']"));
link.click();
Set allWindows = driver.getWindowHandles();
for (String window : allWindows) {
if (!window.equals(parentWindow)) {
driver.switchTo().window(window);
driver.findElement(By.id("submit")).click();
driver.close();
}
}
driver.switchTo().window(parentWindow);
driver.quit();
}
}
πΉ Handling Multiple Windows Using Iterator
import java.util.Iterator;
import java.util.Set;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class WindowHandlingWithIterator {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
driver.findElement(By.xpath("//a[@target='_blank']")).click();
Set handles = driver.getWindowHandles();
Iterator it = handles.iterator();
String parent = it.next();
String child = it.next();
driver.switchTo().window(child);
driver.close();
driver.switchTo().window(parent);
driver.quit();
}
}
πΉ Handling Tabs in Selenium (Using JavaScriptExecutor)
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.Set;
public class HandleTabs {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
((JavascriptExecutor) driver).executeScript("window.open()");
Set windows = driver.getWindowHandles();
String[] windowArray = windows.toArray(new String[0]);
driver.switchTo().window(windowArray[1]);
driver.get("https://www.google.com");
driver.close();
driver.switchTo().window(windowArray[0]);
driver.quit();
}
}
πΉ Switching Windows by Title
public void switchToWindowWithTitle(String expectedTitle) {
Set allWindows = driver.getWindowHandles();
for (String window : allWindows) {
driver.switchTo().window(window);
if (driver.getTitle().equals(expectedTitle)) {
break;
}
}
}
πΉ Handling Authentication Popups
String username = "admin";
String password = "admin123";
driver.get("https://" + username + ":" + password + "@example.com");
πΉ Key Takeaways
getWindowHandle()
→ Current window IDgetWindowHandles()
→ All open windowsswitchTo().window()
→ Switch between tabs/windowsclose()
→ Close current windowquit()
→ Close all windows
❓ Frequently Asked Questions
What is the difference between close() and quit()?
close()
closes the current window. quit()
closes all browser instances and ends the session.
How to handle pop-ups in Selenium?
Use window handles or alerts depending on whether it's a browser window or JavaScript alert.
Is switching between tabs same as windows?
Yes. Selenium treats both tabs and windows similarly using window handles.
Tags: Selenium WebDriver, Java Selenium, Multiple Windows, Window Handle, Automation Testing, QA Interview, Selenium Tabs, Selenium Popups