Handling IFrames in Selenium Webdriver
Handling IFrames in Selenium Webdriver
(https://www.guru99.com/)
IFrame is a web page which is embedded in another web page or an HTML document
embedded inside another HTML document.
The IFrame is often used to insert content from another source, such as an advertisement,
into a Web page. The <iframe> tag specifies an inline frame.
2. How do switch over the elements in iframes using Web Driver commands:
Observe the below image, Advertisement being displayed is an Iframe, we cannot locate or
recognize that by just inspecting using Firebug. So the question is how can you identify the
iframe?
https://www.guru99.com/handling-iframes-selenium.html 1/13
12/13/2018 Handling iFrames in Selenium Webdriver
(/images/Sap-QM/122315_0943_HandlingIfr1.png)
Right click on the element, If you find the option like 'This Frame' then it is an iframe.
(Please refer the above diagram)
Right click on the page and click 'View Page Source' and Search with the 'iframe', if you
can find any tag name with the 'iframe' then it is meaning to say the page consisting an
iframe.
In above diagram, you can see that 'This Frame' option is available upon right clicking, so
we are now sure that it is an iframe.
By Index
By Name or Id
By Web Element
Index is one of the attributes for the Iframe through which we can switch to it.
driver.switchTo().frame(0);
driver.switchTo().frame(1);
Name and ID are attributes of iframe through which we can switch to the it.
driver.switchTo().frame("iframe1");
driver.switchTo().frame("id of the element");
Let's take an example of iframe displaying in the below image. Our requirement is to click
the iframe.
(/images/Sap-QM/122315_0943_HandlingIfr2.png)
Step 1)
driver.get("http://demo.guru99.com/test/guru99home/");
driver.manage().window().maximize();
Step 2)
https://www.guru99.com/handling-iframes-selenium.html 3/13
12/13/2018 Handling iFrames in Selenium Webdriver
driver.switchTo().frame("a077aa5e");
In this step we need to find out the id of the iframe by inspecting through Firebug.
Then switch to the iframe through ID.
Step 3)
driver.findElement(By.xpath("html/body/a/img")).click();
driver.manage().window().maximize();
driver.switchTo().frame("a077aa5e"); //switching the frame by ID
Output:
Browser navigates to the page consisting the above iframe and clicks on the iframe.
driver.switchTo().frame(WebElement);
https://www.guru99.com/handling-iframes-selenium.html 4/13
We have
12/13/2018 to come out of the iframe. Handling iFrames in Selenium Webdriver
To move back to the parent frame, you can either use switchTo().parentFrame() or if you
want to get back to the main (or most parent) frame, you can use
switchTo().defaultContent();
driver.switchTo().parentFrame();
driver.switchTo().defaultContent();
How to switch over the frame, if we CANNOT switch using ID or Web Element:
Suppose if there are 100 frames in the page, and there is no ID available, in this case, we
just don't know from which iframe required element is being loaded (It is the case when we
do not know the index of the frame also).
The solution for the above concern is, we must find the index of the iframe through which the
element is being loaded and then we need to switch to the iframe through the index.
Below are the steps for finding the index of the Frame by which the element is being loaded
by using below snippet
Step 1)
Step 2)
The above code finds the total number of iframes present inside the page using the
tagname 'iframe'.
Step 3)
Objective for this step would be finding out the index of iframe.
https://www.guru99.com/handling-iframes-selenium.html 5/13
12/13/2018 Handling iFrames in Selenium Webdriver
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();}
Above "forloop" iterates all the iframes in the page and it prints '1' if our required iframe was
found else returns '0'.
Output:
Verify the output, you can find the series of 0's and 1's.
Wherever you find the '1' in output that is the index of Frame by which the element is
being loaded.
https://www.guru99.com/handling-iframes-selenium.html 6/13
Since
12/13/2018 the index of the iframe starts with '0' iFrames
Handling if youinfind 1 in the 1stplace, then the index
theWebdriver
Selenium
is 0.
If you find 1 in 3rd place, the index is 2.
We can comment out the for loop, once we found the index.
Step 4)
driver.switchTo().frame(0);
Once you find the index of the element, you can switch over the frame using above
command.
driver.switchTo().frame(index found from the Step 3);
Step5)
driver.findElement(By.xpath("html/body/a/img")).click();
The above code will clicks the iframe or element in the iframe.
https://www.guru99.com/handling-iframes-selenium.html 7/13
Output:
12/13/2018 Handling iFrames in Selenium Webdriver
Browser navigates to the page consisting the above iframe and clicks on the iframe.
At first we must switch to the outer frame by either Index or ID of the iframe
Once we switch to the outer frame we can find the total number of iframes inside the
outer frame, and
We can switch to the inner frame by any of the known methods.
While exiting out of the frame, we must exit out in the same order as we entered into it from
the inner frame first and then outer frame.
(/images/Sap-
QM/122315_0943_HandlingIfr3.png)
The Html code for the above nested frame is as shown below.
(/images/Sap-
QM/122315_0943_HandlingIfr4.png)
https://www.guru99.com/handling-iframes-selenium.html 8/13
The above
12/13/2018 HTML code clearly explains Handling
the iframe
iFramestag (highlighted
in Selenium Webdriver in green) within another
Below are the steps for switching to outer frame and printing the text on outer frames:
Step 1)
Once we switch to the outer frame, we should know whether any inner frame present inside
the outer frame
Step 2)
size = driver.findElements(By.tagName("iframe")).size();
// prints the total number of frames inside outer frame
System.out.println("Total Frames --" + size);
Step 3)
https://www.guru99.com/handling-iframes-selenium.html 9/13
12/13/2018 Handling iFrames in Selenium Webdriver
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);
Output:
The output of the above code would print the text in the Inner frame and Outer frame.
Next (/cross-browser-testing-using-selenium.html)
Upload Selenium Script to XSLT Report in Selenium How to Upload & Download
https://www.guru99.com/handling-iframes-selenium.html 10/13
(/selenium-github.html)
12/13/2018 (/xslt-report- (/upload-download-file-
Handling iFrames in Selenium Webdriver
Selenium Tutorials
24) Introduction to TestNG Groups (/introduction-testng-groups.html)
(https://www.facebook.com/guru99com/)
(https://twitter.com/guru99com)
(https://www.youtube.com/channel/UC19i1XD6k88KqHlET8atqFQ)
(https://forms.aweber.com/form/46/724807646.htm)
About
About US (/about-us.html)
Advertise with Us (/advertise-us.html)
Write For Us (/become-an-instructor.html)
Contact US (/contact-us.html)
Career Sugges on
SAP Career Suggestion Tool (/best-sap-module.html)
Software Testing as a Career (/software-testing-career-
complete-guide.html)
Certificates (/certificate-it-professional.html)
Interes ng
Books to Read! (/books.html)
Suggest a Tutorial
Blog (/blog/)
https://www.guru99.com/handling-iframes-selenium.html 12/13
12/13/2018 Quiz (/tests.html) Handling iFrames in Selenium Webdriver
Review (/best-ergonomic-mouse.html)
Execute online
Execute Java Online (/try-java-editor.html)
Execute Javascript (/execute-javascript-online.html)
Execute HTML (/execute-html-online.html)
Execute Python (/execute-python-online.html)
https://www.guru99.com/handling-iframes-selenium.html 13/13