Handle Multiple Windows using Selenium WebDriver

Handling multiple windows in Selenium WebDriver is very Simple!

Let’s understand this by example: Sometime when we click on a link using Selenium Webdriver, it opens a new window. Selenium doesn’t know about this new window as it is still focusing on Main window. In order to perform actions on new window, we need to switch the selenium control to the new window.

How do we switch the Selenium control to New Window ?

Every web page has a unique Id and Selenium keeps track of all windows opened during current session. We will use those unique window Ids to switch the control to New Window.

Selenium has following inbuilt functions to get the window Ids.

getWindowHandle() – Returns the Id of Main Window on which selenium is currently focusing.

getWindowHandles() – Returns the Ids of all windows opened during current session.

Once we get the Id of New Window then we need to call switchTo() function to switch the control to the new window using following command:


driver.switchTo().window("{New/Tab Window Id}");

How to switch the Selenium Control back to Main Window ?

There are cases where user have to switch the control back to Main window after completing testing on tab window. In that case also, you have to use the switchTo() function to switch the control back to Main Window. You just need to pass the Id of Main window as parameter as shown below. After executing below command, you will be able to perform selenium actions on Main Window again.


driver.switchTo().window("{Main Window Id}");

Complete script:

import java.util.Iterator;
import java.util.Set;
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.Test;

public class MultipleWindows {

	@Test
	public void multipleWinTest() throws Exception {
		System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir") + "/driver/geckodriver.exe");
		// Initialize driver Object
		WebDriver driver = new FirefoxDriver();

		// Maximize Window
		driver.manage().window().maximize();

		// Open Website under test
		driver.get("http://automate-apps.com/contents/");

		// Add Implicit wait
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

		// Click on link which opens a new Tab
		driver.findElement(By.id("why-selenium")).click();

		// Wait for 1 second
		Thread.sleep(1000);

		// Find Id of main window
		String mainWindowId = driver.getWindowHandle();

		// Find Ids of all windows opened during selenium session.
		Set<String> set = driver.getWindowHandles();

		// Iterate set to find out the Id of Tab window
		Iterator<String> iter = set.iterator();

		String tabWindow = null;

		// Compare Ids stored in a Set with MainWindowId using while Loop.
		// As soon you find Id which is not equals to Main Window.
		// Run switchTo command in order to switch the control to that window
		while (iter.hasNext()) {
			tabWindow = iter.next();
			if (!mainWindowId.equals(tabWindow)) {
				// Switch the control to Tab Window
				driver.switchTo().window(tabWindow);
			}
		}

		// Print Heading of a Tab Window.
		System.out.println(driver.findElement(By.className("entry-title")).getText());

		// Close Tab Window
		driver.close();

		// Switch control back to Main Window
		driver.switchTo().window(mainWindowId);

		// Print Heading of Main Window
		System.out.println(driver.findElement(By.className("entry-header")).getText());

		// Close Main Window
		driver.close();

	}
}

If you really like the information provided above, please don’t forget to hit a like on Facebook Page, you can also leave a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *