getWindowHandle() vs getWindowHandles() in Selenium WebDriver | A Comprehensive Step-by-Step Tutorial
getWindowHandle() vs getWindowHandles() in Selenium WebDriver
getWindowHandle() vs getWindowHandles() in Selenium WebDriver

This tutorial will briefly discuss the difference between getWindowHandle() and getWindowHandles() with a sample project.

getWindowHandle() returns the window handle of the current or active window.

getWindowHandles() returns a set of window handles for all the windows opened by the WebDriver instance.

Example:

package TestAutomationCentral;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.Set;

public class SeleniumWindowHandles {
public static void main(String[] args) {
//TestAutomationCentral.com
System.setProperty(“webdriver.chrome.driver”, “C:\\temp\\drivers\\chromedriver.exe”);
WebDriver driver = new ChromeDriver();
driver.get(“https://www.google.com/”);

driver.switchTo().newWindow(WindowType.TAB);
driver.get(“https://testautomationcentral.com/”);

String window = driver.getWindowHandle();
Set<String> windows = driver.getWindowHandles();
System.out.println(window);
System.out.println(windows);
}
}

Output:

CDwindow-44DBE2542BBE46B951F19DB100A4815C
[CDwindow-12BBDF16CDD4588080285A52C884FB9F, CDwindow-44DBE2542BBE46B951F19DB100A4815C]

Share

Leave a Reply