Handling multiple windows or tabs using Selenium WebDriver

We can handle multiple windows in selenium webdriver using Switch To methods which will allow us to switch control from one window to another window.If you are working with web applications then you must have faced this scenario where you have to deal with multiple windows.

If you have to switch between tabs then also you have to use the same approach. Using switch To method we can also handle frames and alerts with easy methods. I will focus on multiple windows as of now.

Before starting this section I will recommend you watch List and Set in Java which will help you to understand this concept in details.



In Selenium, we have the feature that we can get the window name of the current window.In Selenium, we have the getWindowName method that will return current window name in String form.

Approach to handle multiple windows in selenium webdriver


We also have getWindowNames, which will return Set<String> it means the set of window name then we can iterate using Iterator. The set is part of Java collection which allows us to handle multiple sets of data dynamically.
Scenario 1- In some application, you will get some Pop up that is nothing but separate windows takes an example of qaearth practice page.
Therefore, in this case, we need to close the popup and switch to the parent window.

Complete program to handle multiple windows in selenium webdriver

First, we will switch to child window (pop up window) then we will close it and then we will switch to the parent window.

package qaearth;
import java.util.Iterator;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class TestQaEarth {
@Test
public void TestNewWindow() throws InterruptedException {
// Open browser
System.setProperty("webdriver.chrome.driver", "Drivers/chromedriver.exe");
ChromeDriver driver = new ChromeDriver();
// Maximize browser
driver.manage().window().maximize();
// Load app
driver.get("https://qaearth.blogspot.in/p/testing-examples.html");
driver.findElement(By.xpath("//*[text()='Open Window 1']")).click();
// This will return the number of windows opened by Webdriver and will
// return Set of Strings
Set<String> windowHandles = driver.getWindowHandles();
// Now we will iterate using Iterator
Iterator<String> it = windowHandles.iterator();
// It will return the parent window name as a String
String parentBrowser = it.next();
// It will return the child window name as a String
String childBrowser = it.next();
//switch to child window
driver.switchTo().window(childBrowser);
//Once actions done on child window can use close() method to close current window
driver.close();
//switch to parent window to do rest activities
driver.switchTo().window(parentBrowser);

}
}

Instead of Set , we can use List as well as shown in below snippet.

 ArrayList<String> noofwindows =new ArrayList<String>(driver.getWindowHandles());
 System.out.println(noofwindows.size());
 driver.switchTo().window(noofwindows.get(1).toString());