Selenium

How to Handle Multiple Windows or Tabs in Selenium WebDriver – Complete Guide

How to Handle Multiple Windows or Tabs in Selenium WebDriver

By Bhau Automation • Selenium WebDriver Tutorial

🎯 What You Will Learn

  • What are Multiple Windows and Tabs?
  • Difference Between Window and Tab
  • getWindowHandle() Method
  • getWindowHandles() Method
  • Switch Between Browser Windows
  • Close Child Window and Return to Parent
  • Real-Time Selenium Examples
  • Selenium Interview Questions
💡 Handling multiple browser windows and tabs is one of the most frequently asked Selenium WebDriver interview topics. Every Automation Tester should master this concept.

📌 What are Multiple Windows in Selenium?

Many web applications open new browser windows or tabs after clicking a button or link. Selenium WebDriver provides methods to switch control from one browser window to another using unique window handles.

📌 Difference Between Window and Tab

Feature Window Tab
Browser Instance Separate Browser Window Inside Same Browser
Window Handle Unique Unique
Handling Method Same Selenium API Same Selenium API

📌 getWindowHandle() vs getWindowHandles()

Method Purpose
getWindowHandle() Returns the current window handle.
getWindowHandles() Returns all opened window handles.

💻 Selenium Code Example

WebDriver driver = new ChromeDriver();

driver.get("https://www.bhauautomation.com");

String parentWindow = driver.getWindowHandle();

driver.findElement(By.linkText("Open New Window")).click();

Set windows = driver.getWindowHandles();

for(String window : windows){

    if(!window.equals(parentWindow)){
        driver.switchTo().window(window);

        System.out.println(driver.getTitle());

        driver.close();
    }
}

driver.switchTo().window(parentWindow);

🎯 Real-Time Use Cases

  • Payment Gateway Windows
  • Google Login Popup
  • Facebook Login Window
  • PDF Preview Window
  • Terms & Conditions Pop-up
  • Social Media Share Windows

🏆 Best Practices

  • Always store the parent window handle.
  • Switch only after the new window is opened.
  • Close only child windows when required.
  • Return control to the parent window.
  • Use Explicit Wait for dynamic popups.

❓ Selenium Interview Questions

Q1. Which method returns the current window handle?

getWindowHandle()

Q2. Which method returns all browser windows?

getWindowHandles()

Q3. How do you switch to another browser window?

driver.switchTo().window(windowHandle)

Q4. Why should you save the parent window handle?

To switch back after completing operations on child windows.

Q5. Can Selenium handle multiple tabs?

Yes. Selenium treats browser tabs and windows using the same window handle mechanism.

🎥 Watch Complete Video Tutorial

Watch How to Handle Multiple Windows or Tabs in Selenium WebDriver

🔥 Key Takeaways

  • Use getWindowHandle() for the current window.
  • Use getWindowHandles() for all open windows.
  • Always save the parent window before switching.
  • Switch using driver.switchTo().window().
  • Handling multiple windows is a common Selenium interview topic.
⚡ Master Selenium Window Handling concepts to confidently automate real-world applications and crack Selenium Automation interviews.

🚀 Created with ❤️ by Bhau Automation