How to handle random popups when sequence is not the same. | Selenium Forum
M
Posted on 23/10/2015
Hello,

I have come accross a scenario where sequence of popups are changing every time in Selenium WebDriver.
Those are window based popups
I have to validate the answers in the popups but it is not fixed in which occurrence the answer will come (sometime on 1st occurrence, sometime in 3rd or 4th.)

M
Replied on 23/10/2015

As I understand, you said "You do not know on which occurrence popup will come. so first you shall verify if current popup is the right one or not if yes then verify something on that"

Approach should be like this

String parentWindowHandler = driver.getWindowHandle(); // Store your parent window
String subWindowHandler = null;

Set<String> handles = driver.getWindowHandles(); // get all window handles
Iterator<String> iterator = handles.iterator();
while (iterator.hasNext()){
subWindowHandler = iterator.next();
}
driver.switchTo().window(subWindowHandler); // switch to popup window
if (driver.gettitle).equals("expected") if true then
{
// perform operations on popup
}

driver.switchTo().window(parentWindowHandler); // switch back to parent window

if