Multiple Window Handling in Selenium

Today most of the web applications contain multiple navigation links which lead the user to other windows. It is quite possible that at the same time, functionality is performed using two or more windows.  This can be quite a tricky situation although it might sound a simple one. This was something I faced while automating a web application, as working with multiple windows (sometimes even 3-4 windows at the same time) was an essential part of this application).

Selenium WebDriver assigns an alphanumeric id to each window as soon as the WebDriver object is instantiated. This unique alphanumeric id is called window handle. Selenium uses this unique id to switch control among several windows. In simple terms, each unique window has a unique ID, so that Selenium can differentiate when it is switching controls from one window to the other. I will explain it using below example-

public void multipleWindowsHandle() {
        // Store the first window handle
        String firstWindow = driver.getWindowHandle();
        String newWindow = "";
        driver.findElement(By.cssSelector(".example a")).click();
        //You might need to add wait here to wait for second window to appear
        Set<String> allWindows = driver.getWindowHandles();
        //iterate in window handles and switch to the different window
        for (String window : allWindows) {
            if (!window.equals(firstWindow)) {
                newWindow = window;

        if(driver.getTitle().equals("New Window"))){
          System.out.println("You are now on new window");


Following is the expected behavior from above code-

  • Visit the page
  • Click to open a new window
  • Switch between the windows
  • Check the page title to make sure the correct window is in focus

First we have stored single window handle and after click operation we are storing all window handle. Once we have all window handle, we are switching to expected window. Important thing to understand here is that method getWindowHandles() returns a Set. A set never store data in sorted manner(its random), so don’t make a mistake of accessing window handles on basis of index.


Now when we click on link in above scenario, based on the network speed it might took sometime to open the new window. So following can be some solutions-

  1. Thread.Sleep() could be one solution but we never know the exact time to wait. It also depends on internet speed and processor speed.
  2. Wait till total window handles become two.

Either we can use inbuilt method waitForNumberOfWindowsToBe() of ExpectedConditions class or we can make our own as below-

Code to wait for specific number of windows-

public void waitForNumberOfWindowsToEqual(final int numberOfWindows)               { //Making a new expected condition   
new WebDriverWait(driver,30){}.until(new ExpectedCondition<Boolean>(){   
                   @Override public Boolean apply(WebDriver driver) {

                              return (driver.getWindowHandles().size() == numberOfWindows);}



If you handle this wait in your code, your code become more robust. This is how you can work on multiple windows in Selenium WebDriver.

Happy Learning 🙂


Handling Waits in Selenium WebDriver

While testing the websites, we can’t really quantify the exact response time which an action takes on a web page. So we need to handle the waits between actions on a web page while writing selenium scripts.

So lets see how can we implement a wait strategy which is robust, stable for long run for the scripts. If we think of a Selenium script, it is nothing but a JAVA program (assuming you are implementing it in JAVA); So  what about pausing a java program for some time. So one way is following –



But, as I said above we can’t really quantify the time, or we can give maximum time here. But then you will end up spending a lot more time in execution, but purpose of automation is to minimize time. Lets see what are the issues with above strategy-

  1. You can never predict the exact wait time
  2. It increases overhead
  3. It may not work well with all computers

So to handle this, Selenium provides two types of wait strategies-

  1. ImplicitlyWait
  2. WebDriverWait


As we know in Selenium every driver.findElement() command looks for the WebElement in the web page. Implicitlywait tells webdriver to wait for an element if they are not immediately  available, So that Webdriver does not throw NoSuchElementException immediately


So the difference between this and sleep strategy is that here maximum threshold is 20 seconds (can be as per your choice). So if element is available in lets say 4 seconds, it wont wait for other 16 seconds. and the beauty is you dont have to write this command again and again, once you write this command, it is set up for complete WebDriver session.

WebDriver Wait or ExplicitWait-

Many times, we need to explicitly wait for some condition to finish and then to proceed the execution. So this time its not only about waiting for an element to come in DOM. so here is what you have to do for it-

WebDriverWait wait=new WebDriverWait(driver, 30);
//Now use this object to apply wait with conditions
wait.until(ExpectedConditions.textToBePresentInElement(driver.findElement(By.xpath(“”)), “expected text”));

So here, webdriver will poll every 500 seconds to check text in given element and maximum threshold here is 30 seconds. WebDriverWait class provides almost every expected conditions which you can come across while creating script. lets have a look by below snapshot-


Some times we come across situations where the provided expected conditions are not enough and we have to create our own condition. That can be done in following manner-

WebDriverWait wait=new WebDriverWait(driver, 30);
//Now use this object to apply wait with conditions
wait.until(new ExpectedCondition<Boolean>(){
public boolean apply(WebDriver driver){
return boolean_return_type_of_your_condition;

So every expected condition has an Apply method, which you need to override with your custom condition.

So this is how you can handle waits with your Selenium WebDriver scripts. Hope it has helped you understanding and implementing it. 🙂