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

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

Example

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{
   @Test
   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);
      driver.get("https://www.tutorialspoint.com/index.htm");
      String currentwindow = driver.getWindowHandle();
      Set<String> allWindows = driver.getWindowHandles();
      Iterator<String> i = allWindows.iterator();
      while(i.hasNext()){
         String childwindow = i.next();
         if(!childwindow.equalsIgnoreCase(currentWindow)){
            driver.switchTo().window(childwindow);
            System.out.println("The child window is "+childwindow);
         } else {
            System.out.println("There are no children");
         }
      }
      driver.quit();
   }
}
Updated on: 2020-06-10T12:20:39+05:30

15K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements