Iframe handing in Selenium WebDriver
Iframes, in html, are used to divide a web page into multiple sections or to display a web page within another web page.
To interact with any WebElement of a frame. First, we have to switch to that frame and then only WebElements can be identified.
In this tutorial, i will be using word “frame” and “iframe” interchangeably.
Note: Frames are generally not visible using “Firepath”, refer html tab of “Firebug” to identify a frame.
Refer below image to see how frames are defined in a page. Inside a frame, another page is embedded.
<iframe id="iframeResult" frameborder="0">
iframe tag is used to define a frame.
Now to switch to a frame we have three options:
- Switch to a frame with an Index – All the frames in a Web Page are assigned an index starting from 1.
- Switch to a frame with an ID – Most of the time, frames has an ID attribute assigned to it. If a frame has an Id, we can use below method
- Switch to a frame as a Web Element – When a frame does not have an Id, and switch with an Index is never a good way. In that case, we are left with one option and that identifying a frame as a web element and then switch to it. Refer below code for better understanding:
WebElement frame = Driver.findElement(By.className("demo-frame")); Driver.switchTo().frame(frame);
We will be covering practical scenarios on frames in upcoming tutorials.
I hope you enjoyed the article, feel free to write us at firstname.lastname@example.org or email@example.com. Happy Learning 🙂