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.
<html>
<frame1> </frame1>
<frame2> </frame2>
<frame3> </frame3>
</html>

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”>

<head></head>
<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>
</frameset>

</html>

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-identify-frame

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
Example-:
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().frame(“left”);
                                    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 https://dl.dropboxusercontent.com/
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.get(“https://dl.dropboxusercontent.com/u/55228056/Frames.html”);
driver.switchTo().frame(“left”); // indicates frame 1
String lefttext = driver.findElement(By.tagName(“p”)).getText();
System.out.println(lefttext);
driver.switchTo().defaultContent(); // Switch to default (main)
driver.switchTo().frame(2);             // indicates frame 3
driver.findElement(By.name(“name”)); // “name” indicates by value
String righttext = driver.findElement(By.tagName(“p”)).getText();
System.out.println(righttext);
driver.switchTo().defaultContent();     //Switch to default (main)
driver.switchTo().frame(1);                // indicates frame 2 by “id”
String mid = driver.findElement(By.tagName(“p”)).getText();
System.out.println(mid);

Output -:

This left frame                      This is mid frame                         This is right frame

Leave a Reply