Handle iFrame in Selenium WebDriver

In this tutorial, we will discuss what is an iframe and different ways to handle iframe in Selenium Webdriver.

What is iFrame in Selenium WebDriver?

The iframe is also known as Inline Frame is a webpage embedded inside the parent HTML document. It is defined with the <iframe> tag as shown below.

The following is a sample HTML code having two iFrames in it.

Note: If you are trying to perform some operation on an element and you are able to identify the element using XPath showing in Chropath or XPath Helper but using the same in the test is not working and Selenium is throwing a NoSuchElementExceptionexception, you must check whether the element is inside an iframe.

How to Identify an iFrame on a Page?

We cannot identify an iframe a glimpse on the page. You can use the following ways to check whether there is an iframe on the page.

  • Just Right-click on the desired element and check all the options on the menu. If you get options like ‘View Frame source’, ‘Reload Frame’, and ‘This Frame’, it indicates the page includes frames as shown in the following screenshot.

identify iframe in selenium webdriver

  • Right-click on the page and click on View Page Source
  • Use any of the browser inspecting tools to view the DOM Structure

How to Find the Total Number of iFrames on a Web Page?

We can easily get the total iframe count on a page by using the following code.

How to handle iframe in Selenium WebDriver?

I hope you might know the approach of Handing multiple windows in Selenium. Over there we use to switch to the desired window to perform the required operation on it. Likewise, to handle the iframe in Selenium we have to switch to the desired iframe using switchTo.frame() method.We can use this method to switch between different frames on an HTML page.

We can use the switchTo().frame() command in the following ways:

  • switchTo.frame(int frameIndexNumber): The Driver will switch to that specific frame using the frame index number. The index number starts at 0.
  • switchTo.frame(string frameNameOrId): The Driver will switch to that specific frame using frame name or frame ID.
  • switchTo.frame(WebElement frameElement): The Driver will switch to that specific frame by locating the frame element.
  • switchTo().parentFrame(): The Driver will switch to the parent frame of the page.

The following screenshot shows all the commands to switch to desired iframes.

handle iframe in selenium webdriver

Switch to iFrames in Selenium Using Index

The index number of the iframe represents its position on an HTML page. If there are 10 frames on a page and you want to switch to the third iframe, it can be done using the command driver.switchTo().frame(2) as indexing starts at 0.

Note: You should use switching to iframe using index only when you are sure that the desired element is inside the particular iframe index.If you are not sure please use any of the other available the methods.

Switch to iFrames in Selenium Using Name or ID

You can use browser inspecting tools to find the iframe tag in which the required element is present. In the following screenshot, the heading element “HTML Iframes” lies inside an iframe that has both attributes ID as well as NAME. Usually, ID and Name values are different however, they are the same here but this may not be in the actual application.

identify iframe in selenium webdriver by frame id or name

Since we have to pass ID or Name as String input so there won’t be any change in the approach of switching to an iframe by using either ID or NAME. We just have to pass the valid ID or NAME attribute value as String  

Switch to iFrames using WebElement in Selenium

We can also switch to the desired iFrame by providing the WebElement to the switchTo() command as shown below.

Switch Back to the Main Frame

We can directly switch to the parent iframe by simply using driver.switchTo().parentFrame() command.

Switch Back to Main Page

Don’t forget to switch back to the main page once you are done with the operations on iFrames. You can switch back to the main page using the switchTo().defaultContent()

Example Showing all the Above Methods

Test Scenario

  1. Launch the browser
  2. Open the page https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_iframe
  3. Print the total number of iframes on the page
  4. Switch to the first frame and print the page heading
  5. Switch to another frame using WebElement
  6. Switch back to the parent frame
  7. Again get and print the page heading
  8. Switch to the main page


Total number of iframes are: 7
Embedded Page Title on first iFrame: The iframe element
Button text on 2nd iFrame: LOG IN
Printing again Embedded Page Title on first iFrame: The iframe element


We have seen all possible ways of switching between iframe and switching back to the main page when we are done with operations on iframes.I hope you have enjoyed this article,please don’t forget to share this article and if you have any queries or suggestion please do mention that in the comment box.

Recommended Posts

Leave a Reply