iFrames in Selenium

In Selenium trainingwe will now discuss Frames. 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();
driver.get(“https://onlineitguru.com/onlinetutorials/selenium”);

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

Code:

public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.get(“https://onlineitguru.com/onlinetutorials/selenium”);

//Switch by Index
driver.switchTo().frame(0);
driver.quit();
}

Likewise,  you can  exceute 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″>
<!DOCTYPE HTML>
<html>
<head>
<body>
<iframe width=”200″ height=”200″ src= “demo_iframe.html”>
</body>
</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.get(“Url”);
driver.manage().window().maximize();
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
driver.switchTo().defaultContent();
}
}

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();
driver.get(“https://onlineitguru.com/onlinetutorials/selenium”);
driver.manage().window().maximize();
//int size = driver.findElements(By.tagName(“iframe”)).size();

/*for(int i=0; i<=size; i++){
driver.switchTo().frame(i);
int total=driver.findElements(By.xpath(“html/body/a/img”)).size();
System.out.println(total);
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*******”);
driver.findElement(By.xpath(“html/body/a/img”)).click();

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

Subscribe
to our newsletter

Drop Us A Query

Trending Courses
  • Pega testing training
    PEGA Testing Training
  • Today application testing is the deciding factor to launch the application into the market. And people do not launch the application unless it goes true.

  • blockchain course
    Blockchain Online Course
  • Today many people were enthusiastic, to know the exact details of things happening around him. This can get the proper knowledge on Blockchain.

  • ethical hacking course
    Ethical Hacking Certification Course
  • Zeal to learn ethical hacking is common among college students and IT professionals. Because everybody wants to secure their system from cyber attacks.

  • python training
    Python Online Training
  • Python is a dynamic interrupted language which is used in wide varieties of applications. It is very interactive object oriented and high-level programming language.

  • Tableau Training
    Tableau Training
  • Tableau is a Software company that caters interactive data visualization products that provide Business Intelligence services. The company’s Head Quarters is in Seattle, USA.

  • what is pega
    PEGA Online Training
  • Pega Systems Inc. is a Cambridge, Massachusetts based Software Company. It is known for developing software for Customer Relationship Management (CRM) and Business process Management (BPM).


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