Handling iFrames in Selenium Webdriver

Posted By : Neha Saini | 31-Dec-2018
What is an iframe?
 
 
Iframe is an HTML document which is embedded in another HTML document or iFrames is a web page which is embedded in another web page.
 
iframe></iframe> tag is used to define iFrame in HTML.
 
The iFrames are used to insert content from another source. Eg: insert an advertisement into a web page.
 
 
How to identify the iframe:-
 
 
 
Iframe can be identified by using two ways given below:
 
1. Right click on the web element, if we find “This Frame” in the options then it is an iframe.
2. Right click on the web page and click “View Page Source” and then search “iframe” if find any tag name with iframe then it is “iframe”.
 
 
A total number of Iframe can be identified by using below snippet:
Int size = driver.findElements(By.tagName("iframe")).size()

 
The most effective method to switch over the components in iframes utilizing Web Driver directions:-
 
By using 3 ways we can switch over the element in the frame:
 
    • By Index
    • By Name or Id
    • By Web Element
      
1. Switch to the frame by index:-
 
The index is an attribute for the Iframe and by using index we can switch to the Iframe.
Index starts with ‘0’.
Suppose if there are 80 frames on the page, we can use the index to switch on the Iframe.
driver.switchTo().frame(0);
driver.switchTo().frame(1);

 
2. Switch to the frame by ID or Name: -
 
ID or Name are the attributes of Iframe and by using these attributes we can switch to the iframe.
driver.switchTo().frame("iframe1"
driver.switchTo().frame("id of the element")

 

 
3. Switch to the frame by Web Element:-
 
driver.switchTo().frame(WebElement);

 

 
By using Web Element we can switch to the iframe.
 
 
How to switch back to the Main Frame:-
 
To switch back to the parent frame, we can use 
switchTo().parentFrame()  .
Or if we want to switch to the main frame(or most parent frame), we can use 
switchTo().defaultContent()
 
driver.switchTo().parentFrame();
driver.switchTo().defaultContent();
 
 
 
 
 
Related Tags

About Author

Author Image
Neha Saini

Neha is a QA Engineer in Oodles and have good knowledge of Manual Testing and Automation Testing , always eager to learn new things.

Request for Proposal

Name is required

Comment is required

Sending message..