How to Handle multiple Windows
To handle multiple windows using selenium we have 2 pre-defined methods viz, getWindowHandle() and getWindowHandles().
The output of getWindowHandle() is the object or String of current window , while the output of getWindowHandles() is the Set of all the windows opened at that particular time. getWindowHandle() can be useful to get the object of Parent window while getWindowHandle() gives you parent window as well as child window.
So how to switch to child window? The answer is identify from Set which is child instance, since we have saved Parent window earlier.
Following code snippet will help to switch to child window and do actions on it and to finally close it.
driver.get(“http://demoqa.com/registration;);
driver.manage().window().maximize();
String parent = driver.getWindowHandle(); // This will save the instance of current/parent window
driver.findElement(By.id(“button1”)).click();
Set<String> childWin = driver.getWindowHandles(); // This will save instance of all opened windows
for(String s: childWin){
if(!s.equals(parent)){ // This will identify the child window
driver.switchTo().window(s); driver.manage().window().maximize();
driver.close(); }
}
driver.switchTo().window(parent);
Comments
Post a Comment