How to handle iFrame in Selenium

Related Product On This Page What are iframes in Selenium?February 13, 2026 · 8 min read · Tool Comparison

Related Product

How to handle iFrame in Selenium

Understandinghow to handle iframes in Seleniumis essential for testing modern web application, as many websites embed content using flesh and iframes.

Overview

What are iFrames?

iFrames (inline frames) are HTML elements that embed another HTML document within the current page.

Why handle iFrames in Selenium?

  1. Some critical UI factor (descriptor, ads, widgets) are loaded inside iFrames
  2. Selenium can not directly entree elements within an iFrame without switching circumstance
  3. Avoids mutual test failures likeNoSuchElementException

How to address iFrames in Selenium (syntax)

driver.switchTo () .frame (`` frameNameOrID '');

This article explains what iframes are and explores different methods to interact with them using Selenium WebDriver. It also covers how to use theSwitchTo ()method to swop between shape, supported by clear code examples.

What are iframes in Selenium?

An iframe is too known as an inline frame. It is a tag used in to embed an HTML document within a parent HTML papers. An iframe tag is delimitate using& lt; iframe & gt; & lt; /iframe & gt; tags.

When examine with, you can ’ t directly interact with elements inside aniframeunless you first exchange the r ’ s focus to that iframe. By nonremittal, Selenium only work with constituent in the main papers.

Example: If a button is inside a chatbot iframe, Selenium won ’ t detect it until the focus is switched to that iframe.

Properly handling iframes is essential for honest examination, especially on modern websites with embedded message.

Read More:

Difference between flesh and iframe in Selenium

Frameallows splitting the browser window into sections using the & lt; frameset & gt; tag, typically for layout design.

iFrameis Used to embed outside content (like ads or videos) within a page. It can be positioned freely and floats within the main page layout.

Below is a table summarise the key compare between anatomy and iframe in Selenium:

AspectFrameiFrame (Inline Frame)
DefinitionDivides a webpage into multiple sections, each loading its own document.Embeds another HTML document within the current page.
Usage in SeleniumRequires switching context to interact with elements inside the frame.Also requires swop context before access elements within the iframe.
HTML RelevanceConsidered outdated in modern HTML standard.Widely utilise today for embedding external or interactive content.
Typical Use CasesLegacy websites with split screen layout.Embedded video, function, widgets (like chatbots or login signifier).
Tag Used& lt; frame & gt;& lt; iframe & gt;
SupportLimited in HTML5 and mostly deprecated.Fully supported in HTML5 and across modern browsers.

Note: Frame and frameset tags are deprecate as they are no longer supported by HTML 5.

Here is code snip portrays an HTML page check two iframes & # 8211;

Code Snippet in Raw Format is below

& lt; html & gt; & lt; body & gt; & lt; div & gt; & lt; iframe name= '' iframe1 '' height= '' 50 % '' width= '' 50 % '' src= '' https: //www.browserstack.com '' & gt; & lt; /iframe & gt; & lt; /div & gt; & lt; div & gt; & lt; iframe name= '' iframe2 '' height= '' 50 % '' width= '' 50 % '' align= '' left '' src= '' https: //www.browserstack.com/ '' & gt; & lt; /iframe & gt; & lt; /div & gt; & lt; /body & gt; & lt; /html & gt;

How to identify a Frame on a Page?

For a browser to mechanically start interact with the web page, the browser needs to identify the elements under the frame for. It is possible to name the iframes on a web page in two ways:

  • Right-click on the specific element and check all the options. If you happen an selection likeThis Frame, prospect Frame source or Reload Frame,the page includes frames. Consider the image below as an example.
  • Similar to the first step, right-click on the page and pawl onView Page Source.
  • On the page source, search foriframe-tags. If you find any iframe tags, it means the page includes iframes.

Also Read:

Using the SwitchTo () .frame function

For a browser to act with several elements in iframes, the browser must name all the iframes. For this purpose, we need to use theSwitchTo () .framemethod. This method enable the browser to switch between multiple frames. It can be implement in the following ways:

  1. switchTo.frame (int frame turn): Defining the frame exponent figure, the Driver will switch to that specific physique
  2. switchTo.frame (draw frameNameOrId):Defining the frame element or Id, the Driver will switch to that particular frame
  3. switchTo.frame (WebElement frameElement):Defining the frame web ingredient, the Driver will switch to that particular anatomy

The image below give a glimpse of all the commands & # 8211;

Before understanding the execution of the methods above, let ’ s understand how to identify the entire number of figure in a web page.

There are two ways to do this:

  1. Executing JavaScript
  2. Finding the entire number of elements that have the tag “ iframe ”

Example to regain the number of frames:

WebDriver driver = new ChromeDriver (); driver.get (`` https: // url control i-frames/ ''); //By finding all the web elements using iframe tag List & lt; WebElement & gt; iframeElements = driver.findElements (By.tagName (`` iframeResult '')); System.out.println (`` Total number of iframes are `` + iframeElements.size ()); //By executing a java script JavascriptExecutor exe = (JavascriptExecutor) driver; Integer noOfFrames = Integer.parseInt (exe.executeScript (`` return window.length '') .toString ()); System.out.println (`` No. of iframes on the page are `` + numberOfFrames);

Try Handling Frames in Selenium for Free

Pro tip: Tools like SUSA can handle this autonomously — upload your app and get results without writing a single test script.

Switching Frames in Selenium using Index

An Index bit represents the position of a specific iframe on an HTML page.

Suppose if there are 50 chassis, we can switch to a specific iframe expend its particular index number. One can directly shift to the maiden iframe utilise the commanddriver.switchTo () .frame (0).

Refer to the sample code:

public static emptiness main (String [] args) throws InterruptedException {WebDriver driver = new ChromeDriver (); driver.get (`` /URL receive iframes/ ''); //Switch by Index driver.switchTo () .frame (0); driver.quit ();}

Switching Frames using Name or ID

A name and Idattribute is always relate with the iframe tag. One can view this by visit the page source of a particular web page containing iframes. The image below represents a page source of a sample web page hold iframes.

To switch between an iframe using the name attribute, one can use the switch bid as follows:

WebDriver driver = new ChromeDriver (); driver.get (`` URL ” / ''); // URL OF WEBPAGE HAVING FRAMES //Switch by frame name driver.switchTo () .frame (`` iframeResult ''); //BY frame name driver.quit ();

To switch between an iframe using the Id attribute, one can use the switch command as follows:

WebDriver driver = new ChromeDriver (); driver.get (`` URL ” / ''); // URL of webpage receive frames //Switch by frame name driver.switchTo () .frame (`` iframeResult ''); // Switch By ID driver.quit ();

Switching Frames using WebElement

Another way to switch between soma in selenium is to legislate the WebElement to theswitchTo () command. Here the primary step is to happen the iframe constituent and so pass it to the switch method.

WebDriver driver = new ChromeDriver (); driver.get (`` URL ”); URL OF WEBPAGE HAVING FRAMES //First finding the factor habituate any of locator stratedgy WebElement iframeElement = driver.findElement (By.id (`` iframeResult '')); //now using the switch command driver.switchTo () .frame (iframeElement); driver.quit ();

Learn More:

Talk to an Expert

Switching back to Main Page

The main page is where all the iframes are embedded. After operating on a particular iframe, useswitchTo () .parentFrameto travel back to the page pageUseswitchTo () .defaultContentto shift to the primary/first parent frame. Refer to the sample codification below:

WebDriver driver = new ChromeDriver (); driver.get (`` URL ''); // URL OF WEBPAGE HAVING FRAMES //First discover the element using any of locator strategy WebElement iframeElement = driver.findElement (By.id (`` iFrameResult '')); //now utilise the switch command to switch to chief form. driver.switchTo () .frame (0); //Perform all the required tasks in the frame 0 //Switching back to the main window driver.switchTo () .defaultContent (); driver.quit ();

The aforementioned method will help QAs run automatize test cases inSeleniumquicker and with less effort. Simplify and race up tests farther by running them on existent devices and browsers.

Is your Website Reactive across all device?

Check what your customers see, with ourFREEAntiphonal Checker Tool.

Testing on Real Device Cloud with BrowserStack Automate

When working with iframes in Selenium, it ’ s important to test across to ensure reliable behavior.BrowserStack Automateenables you to run Selenium trial on a wide range ofreal iOS and Android devicesas well as background browsers on their.

By using, you can:

  • Validate iframe handling across different devices and browsers.
  • Ensure iframe-based elements (like engraft appliance or payment gateways) lading and function correctly.
  • Debug issues faster with admittance to log, screenshots, and video recordings of examination sessions.

Existent device testing ensures your iframe interactions comport consistently across platforms, helping you build more stable, user-ready automation scripts.

Conclusion

Correctly handling iframesis crucial for building reliable Selenium, especially on modern websites that embed dynamic or external content.

Understanding how to switch between frames using different methods, such as index, name/ID, or WebElement, can assure accurate element interaction.

With real device testing through platforms like BrowserStack Automate, you can formalize iframe behavior across browsers and devices, ensuring a seamless user experience.

Useful Resources for Selenium

Methods, Classes, and Commands

Configuration

XPath

Locators and Selectors

Waits in Selenium

Frameworks in Selenium

Motley

Best Practices, Tips and Tricks

Design Patterns in Selenium: Page Object Model and Page Factory

Action Class

TestNG and Selenium

JUnit and Selenium

Use Cases

Types of Testing with Selenium

Tags
46,000+ Views

# Ask-and-Contributeabout this topic with our Discord community.

Related Guides

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free