iFrames in Selenium

Click to rate this post!
[Total: 0 Average: 0]

In Selenium trainingwe will now discuss about iFrames in selenium. Now we initially go through

What is an iframe?

An Iframe is an HTML document.  We can embed this iframe in another webpage (or) HTML document (or) embedded in another HTML document.  A web designer can change the iframe content without completely reloading the website. Moreover, for a website, a single web page can have multiple frames. Besides a frame can also have inner frames ( Frame inside a frame). In HTML, it is defined in between <iframe> and </iframe>. And if you inspect a web page, you can easily find an iframe. And to understand the working of different iframes, we need to switch between these iFrames. Moreover, we can use  SwitchTO(). frame command to switch between these Iframes. And we can use this command in three ways :

switchTo().frame(int frame number):

This passes the frame index and the driver will switch to that frame.

switchTo().frame(String frame number (or) id ):

This passes the frame element name (or) Id and the driver will switch to that frame.

SwitchTo().frame(Web Element (or) Frame element):

This passes the frame web element and the driver will switch to that frame

Today a website consists of multiple pages. These pages may (or) may not contain frames. So many people would have a question of

How to identify the iframe?

To identify the frames in the web page, we need to perform the following tasks:

Right Click on the Element you want to identify the presence of iframe

then inspect the element on the web page

And then press Ctrl +F and then search iframes. So through search, if you find the phrase, we can say the element contains iframes. 

besides in a website, we do have many frames. And we can find the frame by searching with the frame name. And in the search, if the frame name is found,  the frame is present on the web page.

As explained above, the web page usually consists of multiple frames. So by seeing the webpage, we cannot say the number of elements contained in it. But through the code, we can get the frame count of the webpage. And  we can get the frame  either through executing JAVA Script (or) to embedding < iframes> in the HTML tag

WebDriver driver = new FirefoxDriver();

//By executing a java script
JavascriptExecutor exe = (JavascriptExecutor) driver;
Integer numberOfFrames = Integer.parseInt(exe.executeScript("return window.length").toString());
System.out.println("Number of iframes on the page are " + numberOfFrames);

//By finding all the web elements using iframe tag
List<WebElement> iframeElements = driver.findElements(By.tagName("iframe"));
System.out.println("The total number of iframes are " + iframeElements.size());

Besides, iframes allows you to switch from one frame to the other. Moreover, we can navigate between the elements using index(). Selenium Online Training Courses suggest you to execute the following methods to  execute between the frames.

Method Description
driver.switchTo().frame(0) Swtiches between frames using Index
driver.switchTo().frame(“iframe1”) Switches between the frames using names.
driver.SwitchTo().frame(“IF1”) Switches between the frames using ID’s
driver.switchTo().frame()  Switches to the frame using Web element


public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();

//Switch by Index

Likewise,  you can  execute the code with the different commands explained above. And if you lag anywhere feel free to contact Selenium online training

As said above, these frames allow you to nest frames inside one another.so now let us discuss regarding the

Nested Frames:

Let us consider two frames as shown below. And our aim is to display the text in these frames.

iFrames in Selenium

In the case of nested frames,

we must first switch to the outer frame either by Index(or) by ID.

Once, we switch to the outer frame, we can find the number of iframes inside outer frames.

And we can switch to inner frames by any one of the known methods.

We can execute the nested frames in HTML as shown below

<div class = "iframe">
<div style = "overflow:auto;">
<div id = "iframewrapper" class="iframewrapper">
<iframe id = "iframeResult" frameborder="0">
<iframe width="200" height="200" src= "demo_iframe.html">

And in Web driver, we can execute the nested frames using the following code

Code :
public class FramesInsideFrames {
public static void main(String[] args) {
WebDriver driver=new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);

int size = driver.findElements(By.tagName("iframe")).size();
System.out.println("Total Frames --" + size);

// prints the total number of frames
driver.switchTo().frame(0); // Switching the Outer Frame
System.out.println (driver.findElement(By.xpath("xpath of the outer element ")).getText());

//Printing the text in outer frame
size = driver.findElements(By.tagName("iframe")).size();
// prints the total number of frames inside outer frame

System.out.println("Total Frames --" + size);
driver.switchTo().frame(0); // Switching to innerframe
System.out.println(driver.findElement(By.xpath("xpath of the inner element ")).getText());

//Printing the text in inner frame

And the last topic , that we need to discuss here

How to switch back to the main frame?

Since a web page consists of multiple pages, there is some situation, where the user needs to switch back to the back page (or) home page. So to move to the back page, we need to SwitchTo().parentFrame(). And if you want to go back to the main page, we need to use switchTo().defaultContent();

How to Switch over the frame if we cannot switch ID (or) Element()?

If the frame does not have the ID (or) element, we need to use frame index() to find the frame. Execute the following code to switch over the frames 

public class SwitchToframe {
public static void main(String[] args) throws NoSuchElementException{
WebDriver driver = new FirefoxDriver();
//int size = driver.findElements(By.tagName("iframe")).size();

/*for(int i=0; i<=size; i++){
int total=driver.findElements(By.xpath("html/body/a/img")).size();
driver.switchTo().defaultContent(); //switching back from the iframe

//Commented the code for finding the index of the element
driver.switchTo().frame(0); //Switching to the frame
System.out.println("********We are switched to the iframe*******");

//Clicking the element in line with Advertisement
System.out.println("*********We are done***************");

to our newsletter

Drop Us A Query

Trending Courses
  • oracle 12c rac | OnlineITGuru
    Oracle RAC Training
  • Oracle is the large vendor in providing the various storge services to the people across the globe. This vendor provides a different amount of storage services to the people across the globe.

  • salesforce lightning training | OnlineITGuru
    Salesforce Lightning Training
  • Developing an application is not a simple and easy task. There are various parameters that the web developer need to take care while developing an application. One of those parameters that the developer needs to take care of is the code reusability.

  • Selenium with python
    Selenium with Python Training
  • As we know, that Selenium with Python Web Browser Selenium Automation is Gaining Popularity Day by Day. So many Frameworks and Tools Have arisen to get Services to Developers.

  • machine learning with python
    Machine Learning with Python Training
  • Over last few years, Big Data and analysis have come up, with Exponential and modified Direction of Business. That operate Python, emerged with a fast and strong Contender for going with Predictive Analysis.

  • Data science with R
    Data Science With R Training
  • Understanding and using Linear, non-linear regression Models and Classifying techniques for stats analysis. Hypothesis testing sample methods, to get business decisions.

  • data science with python
    Data Science with Python Training
  • Everyone starts Somewhere, first you learn basics of Every Scripting concept. Here you need complete Introduction to Data Science python libraries Concepts.

100% Secure Payments. All major credit & debit cards accepted.

Call Now Button