Window Handling in Selenium Running Notes:
Window Handle:
-To identify Parent and Child window we use handles.
-Handle uniquely identifies these parent and Child windows using an window identifier.
-Window identifier is simply an address to a window.
How to get Window Handles:
We can window handles by using below methods
getWindowHandle() : Returns parent window Handle(a unique identifier for a parent window)
//code to get Parent window Handle
String parentWindowHandle = driver.getWindowHandle();
System.out.println("The parentWindowHandle " + parentWindowHandle);
System.out.println("The length of identifier " + parentWindowHandle.length());
getWindowHandles() :Returns all window Handles of both Parent and Child windows.
// All Window handles
Set<String> handles = driver.getWindowHandles();
// Filter your child window handle and do a proper switch to the child window.
for (String handle : handles) {
System.out.println(handle);
//Switching to child window
if (!handle.equals(parentWindowHandle)) {
driver.switchTo().window(handle);
}
}
The above methods() are present in WebDriver class.
98DE5EE72D560FB5D23063C6BA4D4EA4
68D884F5FD86859F982F52B2EF2172D9
Each window has one context
Parent window has a context and then child window has context.
When we move from Parent Window to Child window the context changes,That is why driver is not able to locate
WebElement in the child window and throws org.openqa.selenium.NoSuchElementException:
if we don't want to loose this track of context then we should use switch() method between and child windows.
//Syntax of switch() method.
driver.switchTo().window(handle)
PW CW
click
Hyperlink1 -------Child Window
-- switch()
PW --------CW
Switch()
Hyperlink2
--
Hyperlink3
--
//Program to Practice
package com.naomtech.selenium.windows;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import io.github.bonigarcia.wdm.WebDriverManager;
public class ParentWindowTestingProject {
public static void main(String[] args) throws InterruptedException {
// ChromeOptions class
ChromeOptions chromeOptions = new ChromeOptions();
// Abstract class
WebDriverManager.chromedriver().setup();
// Driver interface
WebDriver driver = new ChromeDriver(chromeOptions);
driver.manage().window().maximize();
driver.get("https://the-internet.herokuapp.com/wi...");
String parentWindowHandle = driver.getWindowHandle();
System.out.println("The parentWindowHandle " + parentWindowHandle);
System.out.println("The length of identifier " + parentWindowHandle.length());
Thread.sleep(10000);
// Locating url "Click Here" from Parent window.
WebElement url = driver.findElement(By.xpath("//a[text()='Click Here']"));
// Opening a child window by clicking on Click Here Url
url.click();
System.out.println("The url is " + url.getText());
Thread.sleep(10000);
// All Window handles
Set<String> handles = driver.getWindowHandles();
// Filter your child window handle and do a proper switch to the child window.
for (String handle : handles) {
System.out.println(handle);
//Switching to child window
if (!handle.equals(parentWindowHandle)) {
driver.switchTo().window(handle);
}
}
// Locating text "New Window" from the Child Window
WebElement text = driver.findElement(By.xpath("//h3[text()='New Window']"));
System.out.println(text.getText());
driver.switchTo().window(parentWindowHandle);
//
Thread.sleep(10000);
driver.quit();
}
}