Saturday, January 11, 2014

Moving Between Windows and Frames


Some web applications have many frames or multiple windows. WebDriver supports moving between named windows using the “switchTo” method:
driver.switchTo().window("windowName");
All calls to driver will now be interpreted as being directed to the particular window. But how do you know the window’s name? Take a look at the javascript or link that opened it:
<a href="somewhere.html" target="windowName">Click here to open a new window</a>
Alternatively, you can pass a “window handle” to the “switchTo().window()” method. Knowing this, it’s possible to iterate over every open window like so:
for (String handle : driver.getWindowHandles()) {
    driver.switchTo().window(handle);
}
You can also switch from frame to frame (or into iframes):
driver.switchTo().frame("frameName");
It’s possible to access subframes by separating the path with a dot, and you can specify the frame by its index too. That is:
driver.switchTo().frame("frameName.0.child");
would go to the frame named “child” of the first subframe of the frame called “frameName”. All frames are evaluated as if from *top*.

No comments:

Post a Comment