Handling Multiple Windows in Selenium WebDriver

πŸ“š Table of Contents

πŸ”Ή 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

MethodDescription
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 ID
  • getWindowHandles() → All open windows
  • switchTo().window() → Switch between tabs/windows
  • close() → Close current window
  • quit() → 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