Double Bonanza Offer - Upto 30% Off + 1 Self Paced Course Free | OFFER ENDING IN: 0 D 0 H 0 M 0 S

Log In to start Learning

Login via

Post By Admin Last Updated At 2020-06-15
iFrames in Selenium

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 . 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 scriptJavascriptExecutor 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 tagList 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.

MethodDescription
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 Indexdriver.switchTo().frame(0);driver.quit();}

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