Skip to content

Instantly share code, notes, and snippets.

@Jaimin180296
Created July 28, 2021 11:39
Show Gist options
  • Save Jaimin180296/3474173fee506cce90aebb123fcf9d67 to your computer and use it in GitHub Desktop.
Save Jaimin180296/3474173fee506cce90aebb123fcf9d67 to your computer and use it in GitHub Desktop.
driver.get("https://demoqa.com/browser-windows");
// Open new child window within the main window
driver.findElement(By.id("tabButton")).click();
//Get handles of the windows
String mainWindowHandle = driver.getWindowHandle();
System.out.println("main" + mainWindowHandle);
Set<String> allWindowHandles = driver.getWindowHandles();
Iterator<String> iterator = allWindowHandles.iterator();
// Here we will check if child window has other child windows and will fetch the heading of the child window
while (iterator.hasNext()) {
String ChildWindow = iterator.next();
if (!mainWindowHandle.equalsIgnoreCase(ChildWindow)) {
System.out.println("child" + ChildWindow);
driver.switchTo().window(ChildWindow);
WebElement text = driver.findElement(By.id("sampleHeading"));
System.out.println("Heading of child window is " + text.getText());
Thread.sleep(2000);
driver.close();
}
driver.switchTo().window(mainWindowHandle);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment