How to apply wait for new window using Selenium WebDriver

As you might have seen the following case during web automation that some time when you perform click on a particular hyper link/button, it opens up a new window. In this post I will be going explain you that how to apply wait for a new window until it gets open. Look at the scenario below.

Scenario :

1. Launch URL :
2. Click on “Why Selenium” hyper link.(As soon as you perform click on it, it opens a new window)
3. Wait for new window.
4. Close browser.

Program :

import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;

public class WaitForNewWindowTest {

	public void testApp() {

		WebDriver driver = new FirefoxDriver();

		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);



		//Verify new window is opened or not
		Assert.assertTrue(waitForNewWindow(driver,10), "New window is not opened");

		System.out.println("New window has been opened");


	public boolean waitForNewWindow(WebDriver driver, int timeout){
	      boolean flag = false;
	      int counter = 0;
	          try {
	              Set<String> winId = driver.getWindowHandles();
	              if(winId.size() > 1){
	            	  flag = true;
	                  return flag;
	              if(counter > timeout){
	                  return flag;
	          } catch (Exception e) {
	        	  return false;
	      return flag;


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

Leave a Reply

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