How to handle popup windows in Selenium?

Software TestingAutomation TestingSelenium Web Driver

Selenium has getWindowHandles() method , which returns all the window handle ids for all the open windows. This is stored in Set data structure in String data types.

In order to navigate to a specific window, we need to traverse to the window we want to access with the iterator() method then switch to that window.

getWindowHandle() method gives the window handle id of the current window Id.


import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import java.util.concurrent.TimeUnit;
import java.util.List;
import java.util.Set;
import java.util.Iterator;
import org.testng.annotations.Test;
public class WindowHandles{
   public void windowHandle() throws Exception {
      System.setProperty("", "C:\Selenium\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      // getting the current window handle id
      String currentwindow = driver.getWindowHandle();
      // getting all the window handles in Set data structure
      Set<String> allWindows = driver.getWindowHandles();
      // traversing each ids with the help of iterator()
      Iterator<String> i = allWindows.iterator();
      //Iterating through the window handle ids
         String childwindow =;
            System.out.println("The child window is "+childwindow);
         } else {
            System.out.println("There are no children");
Updated on 10-Jun-2020 14:54:55