Working with IFrames and frames
Frames are a now deprecated means of building a site layout from multiple documents on the same domain. You are unlikely to work with them unless you are working with an pre HTML5 webapp. Iframes allow the insertion of a document from an entirely different domain, and are still commonly used.
If you need to work with frames or iframes, WebDriver allows you to work with them in the same way. Consider a button within an iframe. If we inspect the element using the browser development tools, we might see the following:
If it was not for the iframe we would expect to click on the button using something like:
However, if there are no buttons outside of the iframe, you might instead get a no such element error. This happens because Selenium is only aware of the elements in the top level document. To interact with the button, we will need to first switch to the frame, in a similar way to how we switch windows. WebDriver offers three ways of switching to a frame.
Using a WebElement
Switching using a WebElement is the most flexible option. You can find the frame using your preferred selector and switch to it.
Using a name or ID
If your frame or iframe has an id or name attribute, this can be used instead. If the name or ID is not unique on the page, then the first one found will be switched to.
Using an index
It is also possible to use the index of the frame, such as can be queried using window.frames in JavaScript.
Leaving a frame
To leave an iframe or frameset, switch back to the default content like so: