What is the difference between getWindowHandle() and getWindowHandles() in Selenium?

getWindowHandle() and getWindowHandles() methods have some prominent differences.

driver.getWindowHandles() – It stores the set of handles for all the pages opened simultaneously.

driver.getWindowHandle() – It fetches the handle of the web page which is in focus. It gets the address of the active browser and it has a return type of String.

Code Implementation


import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import java.util.Set;
import java.util.Iterator;
import org.testng.annotations.Test;
public class WindowHandles{
   public void windowHandle() throws Exception {
      System.setProperty("webdriver.chrome.driver", "C:\Selenium\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      String currentwindow = driver.getWindowHandle();
      Set<String> allWindows = driver.getWindowHandles();
      Iterator<String> i = allWindows.iterator();
         String childwindow = i.next();
            System.out.println("The child window is "+childwindow);
         } else {
            System.out.println("There are no children");

Updated on: 10-Jun-2020

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started