getWindowHandle() and getWindowHandles() methods.

getWindowHandle() stores the reference of the currently active window.

On the other hand, getWindowHandles() returns a list of all the opened windows associated with a particular driver instance.

getWindowHandle()

It basically returns an id(handle) pointing to the currently active window. The return type of this method is a String type. We can use this reference to return back to the same window after switching multiple windows. The basic syntax of writing this statement is given below:

// Stores ref of the current window
        String mainWindow=driver.getWindowHandle();

getWindowHandles()

This method returns a list of all the opened windows associated with the specific driver. The return type of this method is a Set type. Because it eliminates the duplicate instances of the window with the same id if there are such instances present. The basic syntax of writing this statement is given below:

// Getting a list of all opened windows in a set.
         Set windowSet=driver.getWindowHandles();
         System.out.println("Windows Count: "+windowSet.size());

To Traverse between multiple windows, we could use below two approaches.

Method 1 – Switching window using Set and browser title.

  • Use foreach() for traverse for each instance in the Set object
  • Use switchTo() method of WebDriver instance to switch control on each window
  • Use getTitle() method of WebDriver instance to get the title of the currently active window
  • Use if() condition to validate search the expected window via browser title
  • Now, use equalsIgnoreCase() method of String class for comparison
  • If the expected title is found break the foreach loop.
  • Perform the next step in the current window.
for (String object : windowSet) 
	{
	        driver.switchTo().window(object);
		String currentWindowTitle=driver.getTitle();
		System.out.println("currentWindowTitle: "+currentWindowTitle);
			if(currentWindowTitle.equalsIgnoreCase("Basic Archives")) 
			{
				break;
			}
	}

// Click link in new window
	driver.findElement(By.xpath("//a[contains(text(),'Basic')]").click();

Method 2 – Switching window using a List and window index.

  • First of all, we have to convert Set into an ArrayList
  • Create instance of ArrayList and use addAll() to save a copy of values from Set to ArrayList
  • Use the get(index) method to navigate to the required window directly without traversing all windows. But this can be done only if the value of the index is known
  • Perform the next action in the current window.
// Convert Set into an ArrayList
		List windowList=new ArrayList<>();
		windowList.addAll(windowSet);		
		driver.switchTo().window(windowList.get(1).toString());
		
		// Click link in new window
		driver.findElement(By.xpath("//a[contains(text(),\"Basic Java – 1 \")]")).click();

Complete Code using both methods

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Demo 
{
	public static void main(String[] args) throws InterruptedException
	{
		System.setProperty("webdriver.chrome.driver", "c://chromedriver.exe");
		WebDriver driver=new ChromeDriver();
		
		driver.get("http://www.allinoneblogs.com/");
		
		driver.findElement(By.xpath("//img[contains(@title,\"Learn Java\")]")).click();
		
		// Stores ref of the current window
		String mainWindow=driver.getWindowHandle();
		
		// Getting list of all opened windows in a set.
		Set<String> windowSet=driver.getWindowHandles();
		System.out.println("Windows Count: "+windowSet.size());
		
		// Method 1 - Switching window using Set and browser title.
		for (String object : windowSet) 
		{
			driver.switchTo().window(object);
			String currentWindowTitle=driver.getTitle();
			System.out.println("currentWindowTitle: "+currentWindowTitle);
			if(currentWindowTitle.equalsIgnoreCase("Basic Archives")) 
			{
				break;
			}
		}
		
		//Method 2 - Switching window using a List and window index.
		// Convert Set into an ArrayList
		List windowList=new ArrayList<>();
		windowList.addAll(windowSet);
		// Remove comment from below statement to use this method.
		//driver.switchTo().window(windowList.get(1).toString());
		
		// Click link in new window
		driver.findElement(By.xpath("//a[contains(text(),\"Basic Java – 1 \")]")).click();
				
		//Switch back to main window using value returned from getWindowHandle()
		driver.switchTo().window(mainWindow);		
	}
}

TestNG:

Computer Basics:

Java Basics:

Java Collection:

Java File Handling:

OOPs Concept:

Java Question And Answer:

Java Programs:

Selenium Tutorials:

JIRA:

Agile:


1279total visits,4visits today

Leave a Reply

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