Window Handling in Selenium

In this tutorial, we will learn about ‘How To Handle Multiple Windows in Selenium’.

We will all methods to learn window handling in Selenium.

While automating a web application we faced this scenario – after clicking on element it throws multiple window. We can handle multiple window using Selenium by using ‘SwichTo’ and  ‘getWindowHandle’ methods.

 

What is window handling in Selenium?

For switching from one window to another window we store the address of window in a String in Selenium. Each browser window has unique window ID that can uniquely identify it.

We used getWindowhandle() and getWindowhandles() methods to handle the window.

 

Different Methods to handle window in Selenium

  • getWindowHandle() – This method is used to handle the current window.
  • getWindowHandles() – This method is used to handle the all opened window.
  • Set – It is an interface which is used to store the all windows in the form of String.
  • switchTo() – This method is used to switch from one window to other window.
  • Iterator – It is an interface to fetch the element one by one.

Let’s take a scenario where we will switch to child window/tab and get the window title.

Code –

package datapkg;

import java.util.Iterator;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

import junit.framework.Assert;

public class WindowHandle {
    
    
        @Test
        public void testWindowHandle() throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", "E:\\Selenium\\chromedriver_win32\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.w3schools.com/css/css_tooltip.asp");
        driver.manage().window().maximize();
        driver.findElement(By.xpath("//span[@class='ribbon-topnav ws-hide-1066']")).click();
        Thread.sleep(2000);
        
        String parentWin = driver.getWindowHandle();
        
        Set<String> windows = driver.getWindowHandles();
        
        Iterator<String> it = windows.iterator();
        
        while(it.hasNext()) {
            
            String childWin = it.next();
            
            if(!parentWin.equals(childWin)) {
                
            driver.switchTo().window(childWin);
            
            // Verify title of the child window
            System.out.println(driver.getTitle());
            String expectedTitle = "Create a Free Website With W3Schools Spaces";
            
            Assert.assertEquals(driver.getTitle(), expectedTitle);
            
            driver.quit();
            
            }
            
        }
        
    }

}

Code Explanation:

  • Navigate to https://www.w3schools.com/css/css_tooltip.asp
  • Click on Website new link in header section. It will open in new tab.
  • First store parent window using getWindowHandle().
  • String parentWin = driver.getWindowHandle();
  • Store child window using Set interface and getWindowHandles() method.
  • Using iterator we will iterate each window.
  • Using switchTo() method we will switch to the different window.

Watch this Video-

 

FAQ – Frequently Asked Questions

1. Can Selenium handle window application?

No, Selenium only supports to web based application.

2. How do you handle multiple windows in Selenium?

Using getWindowHandle() and getWindowhandles() method we can handle window in selenium.

3. How does Selenium handle attachment?

Using sendKeys() method we can easily upload the file.

Second way is using AutoIt tool we can upload the file.

 

Hope you have enjoyed this post. Happy Learning 🙂

 

How To Select Multiple Checkbox in Selenium

What is Fluent Wait in Selenium

 

Leave a Comment