It’s been quite some time since my last post. Here is problem we face many times. As time goes by coding standard rises and now a developer code in such a way that, it’s difficult to find different elements with names. One such difficulty come, when there is a pop up window appears on clicking a link and that has no name. Even with Selenium IDE, it’s difficult to find the handler name. What to do in such scenarios when we need to do some operation in the pop up window. There is a solution to it. What we have to do is, we need to use getWindowHandles() method that WebDriver provides. It generally returns a set of all the window handle that are active. The set is generally a set of Strings. Below is a sample code that can be used to select a pop up window.
public boolean testNewWindow(){ String currentHandle = driver.getWindowHandle(); Set handles = driver.getWindowHandles(); handles.remove(currentHandle); if (handles.size() > 0) { try{ driver.switchTo().window(handles.iterator().next()); return true; } catch(Exception e){ System.out.println(e.getMessage()); return false; } } System.out.println("Did not find window"); return false; }
Here the variable currentHandle holds the current window identification and the set handles hold all the window handle through which we iterate. Once you are done Hope this will make someone’s life a little easier. 🙂