How to handle popup windows in selenium
In selenium, we have getWindowHandles() method to Handle popup windows in selenium. getWindowHandles() method returns all opened windows ID and store in Set data structure in string data types. To handle multiple windows, we have to switch to a secondary window from the home page.
After opening any website, we have to get the main window handle by using the getWindowHandles() method. The window handle returns an alphanumeric ID. After getting windows ID we have to compare all the windows handles with the main window.

Setting up webdriver:
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
To get window handle:
String popUps = driver.getWindowHandle(); System.out.println(popUps);
Fetching all windows handle size:
Set<String> allWindows = driver.getWindowHandles(); System.out.println(allWindows.size());
Iterating window handles:
java.util.Iterator<String> iterator = allWindows.iterator();
Comparing main window to switch to another window:
String getCurrentWindowsID;
//runs until all the active windows found
while(iterator.hasNext()) {
getCurrentWindowsID = iterator.next().toString();
System.out.println(getCurrentWindowsID);
//Comparing main window
if(!popUps.equals(getCurrentWindowsID)) {
//switching to new popup window
driver.switchTo().window(getCurrentWindowsID);
//sending values into the form field of popup window
driver.findElement(By.id("Initial")).sendKeys("SA");
driver.findElement(By.id("FirstName")).sendKeys("Santosh");
driver.findElement(By.id("LastName")).sendKeys("Adhikari");
Thread.sleep(5000);
//closing driver
driver.close();
}
getWindowHandles(): This method returns window handle ID of current window.
Example code for handling multiple windows in selenium
Note: we are going to use Java programming language
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class PopUp {
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://demosite.executeautomation.com/Login.html");
driver.manage().window().maximize();
driver.findElement(By.xpath("//*[@id=\"userName\"]/p[3]/input")).click();
//to get main window handle
String popUps = driver.getWindowHandle(); System.out.println(popUps);
//click on login button
driver.findElement(By.xpath("//*[@id=\"details\"]/div[1]/p/a")).click();
// getting total windows size
Set<String> allWindows = driver.getWindowHandles();
System.out.println(allWindows.size());
//Iterating windows
java.util.Iterator<String> iterator = allWindows.iterator();
String getCurrentWindowsID;
//runs until all the active windows found
while(iterator.hasNext()) {
getCurrentWindowsID = iterator.next().toString();
System.out.println(getCurrentWindowsID);
//Comparing main window
if(!popUps.equals(getCurrentWindowsID)) {
//switching to new popup window
driver.switchTo().window(getCurrentWindowsID);
//sending values into the form field of popup window
driver.findElement(By.id("Initial")).sendKeys("SA");
driver.findElement(By.id("FirstName")).sendKeys("Santosh");
driver.findElement(By.id("LastName")).sendKeys("Adhikari");
Thread.sleep(5000);
//closing driver
driver.close();
}
}
}
}
Selenium official documentation
