Frame in Selenium Webdriver

What is Frame -:
A webpage / page of any application contain number of elements. These elements may present in different frames. Frame in selenium webdriver is a HTML documents embedded inside another HTML documents. Also web pages can be embedded in another web page.
<frame1> </frame1>
<frame2> </frame2>
<frame3> </frame3>

It is also called as Inline Frame (Iframe).

Main (Default) Page Content

This is left frame This is mid frame This is right frame

How to identify frames in selenium WebDriver-:
In web pages it is not easy to detect frames in web page.In Selenium to work with Iframe / Frames, we have different ways to identify frame depending on the need. Following are the ways of identify frames
A] While inspecting the HTML code of web page then get to know how many frames in web page

<html webdriver = “true”>

<frameset border=”0″ framespacing=”0″ frameborder=”NO” cols=”25%,*,25%”>
<frame id=”left” src=”frame_a.htm”></frame>
<frame src=”frame_b.htm”></frame>
<frame src=”frame_c.htm” name=”right”></frame>


B] Click on element of web page
1] Right click on the element then will get whether frame is there or not. If frame option is present it means web page contains frame.
2] Select frame information according to requirement Iframe.


How to handle frames in selenium WebDriver-:
We can handle and switch over the elements in frames using 3 ways.
– By Index
– By Name or Id
– By Web Element
driver.switchTo().defaultContent();           // Deafault or main
driver.switchTo().frame(“left”);                  // left indicates id
driver.switchTo().frame(2);                       // 2 indicates index
driver.switchTo().frame(1);                       // 1 indicates index

How frame works in selenium WebDriver -:
How to switch to frames in selenium webdriver -:
If there are three frames in webpage in default contents then
1] If we are working in first (left) frame and once finished the work then have to switch in default contents of web page using
                                    driver.switchTo().defaultContent();     // Switching to default content

2] Work same like first frame

3] Work same like first or second frame

After work done of respective frame, then have to switch in default which main content and then go to next frame where you want to go from default or main page.

Example -:
Based on
package Element;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class frame {

public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.switchTo().frame(“left”); // indicates frame 1
String lefttext = driver.findElement(By.tagName(“p”)).getText();
driver.switchTo().defaultContent(); // Switch to default (main)
driver.switchTo().frame(2);             // indicates frame 3
driver.findElement(“name”)); // “name” indicates by value
String righttext = driver.findElement(By.tagName(“p”)).getText();
driver.switchTo().defaultContent();     //Switch to default (main)
driver.switchTo().frame(1);                // indicates frame 2 by “id”
String mid = driver.findElement(By.tagName(“p”)).getText();

Output -:

This left frame                      This is mid frame                         This is right frame

Leave a Reply