How to Multiple Window Handling in Selenium WebDriver?

Switch to Multiple Window || Window Handling In Selenium Example Program: How to Switch to Multiple Window Using Java Selenium Program?

In today’s post, we are going to discuss one of the common scenarios which you may face during the interview that is appearing in multiple windows when you click on some elements on a web page.

Sometimes the focus is still on the current browser, but when you try to do some operation, it will not work, and you will get some errors. You are getting such errors because the selenium webdriver is not able to uniquely identify the opened browser.

This is happening because when we open any browser window that time selenium webdriver assigns a unique value to that window for uniquely identify that window, but when multiple windows are opens that time selenium webdriver is not able to locate the browser window.

To Handle multiple browser windows, Selenium webdriver interface provides two methods, that are:

  • getWindowHandle(): This method will return a string value of the current parent browsers window handle.
  • getWindowHandles(): This method will return a set of all windows handles that are open in that session.

With these two methods, we can use the switchTo() method to switch from one window to another window by providing the different window handle.

Window Handling In Selenium

package com.selenium.switchto;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class Switchto_MultipleWindow 
   WebDriver driver=new FirefoxDriver();
   public void open()
      driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS);
   public void operation() throws InterruptedException
      String parent=driver.getWindowHandle();
      ArrayList<String> handles=new ArrayList<String>(driver.getWindowHandles());
      for(int i=0;i<handles.size() ;i++)
         String str=handles.get(i).toString();
            System.out.println(i+ "Parent Popup opened");
            System.out.println("This Is Your Parent Window");
            System.out.println(i+ "Parent Popup closed");
            System.out.println(i +"popup is opened");
            System.out.println(i +"popup is closed");

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to, or You can join me on Linkedin.

1 thought on “How to Multiple Window Handling in Selenium WebDriver?”

  1. Can you plz provide solution How to Switch(Specific windows) to Multiple Window…for examples there is 10 window and I want to switch on 4th one……….while handling the windows….I’m trying this by using get index method of the list but not getting the required output


Leave a Comment