1

When I am using selenium on FirefoxDriver I am switching windows using the following code

final Set<String> allwindowID = driver.getWindowHandles();
        final Iterator<String> itr = allwindowID.iterator();
        while (itr.hasNext()) {
            if (parentID == itr.next()) {
                parentID = itr.next();
            }
            else {
                childID = itr.next();
            }
        }
        driver.switchTo().window(childID);

But the same code is not working when I am using HtmlUnitDriver. Can anybody help ?

1 Answers1

0

You are comparing string references and not string values, use equals() instead of ==.

while (itr.hasNext()) {
    if (parentID.equals(itr.next())) { 
        parentID = itr.next();   
    } else {
        childID = itr.next();
    }
}
driver.switchTo().window(childID);

For details: How do I compare strings in Java?

Also you can re-write same method as

while (itr.hasNext()) {
    if (!parentID.equals(itr.next())) { 
        // No need to reassign same value to parentID  
        childID = itr.next();       
    }
}
driver.switchTo().window(childID);
Community
  • 1
  • 1
Ajinkya
  • 22,324
  • 33
  • 110
  • 161