I have googled and looked at this site for reference but all the answers I came across did not provide a guaranteed way to switch to a specific window.
I am using Java with Selenium and I trying to find a guaranteed way to switch between two windows (where the second window is from clicking a link that produces a pop up).
driver.getWindowHandles()
creates a Set object and since the Set interface does not provide any ordering guarantees, how will I be able to switch to a specific window?
What I currently have is this:
public static void switchToPopUpWindow(By by) {
driver.findElement(by).click();
Set<String> handles = driver.getWindowHandles();
if (handles.size() > 1) {
for (String currentWindow : handles) {
driver.switchTo().window(currentWindow);
}
} else {
logger.info("There is only one window open...");
}
}
However, since the ordering is not guaranteed, it won't always land on the window that I want. How can I guarantee a switch to the pop up window?