In this article we learn the how to handle frames in selenium webdriver and what is an iframe. HTML frames are used to divide browser window into multiple sections where each section can load a separate HTML document. A frame can be created using <frame> tag in html.
How To Handle Frames In Selenium WebDriver?
We can switch to frame/iframe using four ways:
1) Switch to frame/iframe using its id.
2) Switch to frame/iframe using its name.
3) Switch to frame/iframe using its index. -Not useful. If developer make changes in coding, then it will not work.
4) Switch to frame/iframe using its web element. -Useful Method
Syntax:
driver. switchTo (). frame(frame1);
Switching back to parent frame or default content:
1) driver. switchTo (). defaultContent (); (iframe to root html)
It selects either the first frame on the page, or the main document when a page contains iframes.
It returns driver focused on the parent frame.
2) driver. switchTo (). parentFrame (); (iframe to parent iframe)
It changes focus to the parent context. If the current context is the top-level browsing context, the context remains unchanged.
It returns driver focused on the parent frame.
What is an iFrame?
IFrame stands for inline frame which is also a HTML document.
An IFrame is an HTML document embedded inside another HTML document on a website. The IFrame HTML element is often used to insert content from another source, such as an advertisement, into a Web page.
An IFrame can have its own scrollbars and boarders.
We can create an IFrame using <iframe> tag of html.