IFrames in Selenium

IFrames in Selenium

Table of Contents

Iframes in selenium web driver will be the web page or an inline frame which is embedded in another web page or may be an HTML document that is embedded inside the HTML document. The Iframe will be often used to add the content from the sources like an advertisement into a web page. This iframe will be defined as <iframe> tag

We can identify the iframe by just seeing the web page or may be by inspecting Firebug.

How to identify the iframe using Selenium WebDriver

We can identify he frames in the selenium by using the methods by:

  • By right clicking on the element, if we find an option like ‘This Frame’ then it is iframe
  • We should right click on the page and look into ‘view page source’ and search with ‘iframe’ as we can find any tag name with a ‘iframe’ then it will be the page consisting an iframe.

This diagram views that ‘This Frame’ option is available. It is not sure that it its an iframe. 

We can recognise the total number of iframes by using the following syntax

int size = driver.findElements (By.tagName (“iframe”)).size ();

How we have to switch the elements in IFrames in Selenium by using the webDriver commands?

We can shift the elements and manage frames in selenium using 3 modes.

  • By Index
  • By Name or Id
  • By Web Element

We can switch to frame by index

Index is the property for frame handling in Selenium as it is switch to it.

Index of the iframe starts with ‘0’

Suppose if there are 100 frames in page,we can switch to the frame in selenium by using the index.

  • driver.switchTo().frame(0);
  • driver.switchTo().frame(1);

We can switch to the frame by Name or ID

Name and ID are the attributes for handling frames in the selenium through which we can switch to the iframe.

  • driver.switchTo().frame(“iframe1”);
  • driver.switchTo().frame(“id of the element”);

This is not possible to click iframe directly by XPath as this iframe as we can use frame and then click using then XPath.

First we have to initialize the Firefox driver.

Navigate to the required site which consists the iframe.

Maximized window

driver. SwitchTo ().frame(“a077aa5e”);

This will have to find out the id of the iframe by verifying through Firebug.

Then we have to switch to the iframe through the ID.

driver.findElement (By.xpath(“html/body/a/img”)).click();

This is to know out the xpath of the element that need to be selected. Then click the element by using the web driver command. For example

public class SwitchToFrame_ID {

public static void main(String[] args) {

WebDriver driver = new Firefox Driver (); //navigates to the Browser

    driver.get(“http://demo.iitforce.com/test/iitforcehome/”); 

  // goes to the page consisting an iframe

      driver.manage().window().maximize();

      driver.switchTo().frame(“a077aa5e”); //switching the frame by ID

System.out.println(“********We are switch to the iframe*******”);

      driver.findElement(By.xpath(“html/body/a/img”)).click();

       //Clicks the iframe

   System.out.println(“*********We are done***************”);

      }

}

This output is it transfers to the page consisting the above iframe and clicks on the iframe.

Switch to the frame by Web element

We can shift to the iframe by using web element

  • driver.switchTo().frame(WebElement);

How we can switch to the main frame back?

We should come out of the iframe. To move we can either use the switchTo ().parent Frame () or if we want to get back to the main frame, we can use switchTo ().default Content ().

Driver. SwitchTo ().parent Frame ();

  Driver. SwitchTo ().default Content ();

How we can use the Frame where we cannot use Switch using ID or may be Web Element?

Suppose if there are 100 frames in the page and there is no ID we have we can just don’t know from which frame it is required element which is being loaded. The solution for the concern we can know the index of the iframe through which the element will be loaded and also switch to the iframe through the index.

Questions

  1. What is Iframe?
  2. How to switch Iframes between elements in selenium web driver?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share this article
Subscribe
By pressing the Subscribe button, you confirm that you have read our Privacy Policy.
Need a Free Demo Class?
Join H2K Infosys IT Online Training
Enroll Free demo class