How to handle iFrame in Selenium
Related Product On This Page What are iframes in Selenium?February 13, 2026 · 8 min read · Tool Comparison
Understandinghow to handle iframes in Seleniumis essential for testing modern web application, as many websites embed content using flesh and iframes. What are iFrames? iFrames (inline frames) are HTML elements that embed another HTML document within the current page. Why handle iFrames in Selenium? How to address iFrames in Selenium (syntax) 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. 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: 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: 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 – 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: Also Read: 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: 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: Example to regain the number of frames: 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. 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: 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: To switch between an iframe using the Id attribute, one can use the switch command as follows: 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. Learn More: The main page is where all the iframes are embedded. After operating on a particular iframe, useswitchTo () .parentFrameto travel back to the page page. UseswitchTo () .defaultContentto shift to the primary/first parent frame. Refer to the sample codification below: 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. 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: Existent device testing ensures your iframe interactions comport consistently across platforms, helping you build more stable, user-ready automation scripts. 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. 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 # Ask-and-Contributeabout this topic with our Discord community. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.Related Product
How to handle iFrame in Selenium
Overview
driver.switchTo () .frame (`` frameNameOrID '');
What are iframes in Selenium?
Difference between flesh and iframe in Selenium
Aspect Frame iFrame (Inline Frame) Definition Divides a webpage into multiple sections, each loading its own document. Embeds another HTML document within the current page. Usage in Selenium Requires switching context to interact with elements inside the frame. Also requires swop context before access elements within the iframe. HTML Relevance Considered outdated in modern HTML standard. Widely utilise today for embedding external or interactive content. Typical Use Cases Legacy websites with split screen layout. Embedded video, function, widgets (like chatbots or login signifier). Tag Used & lt; frame & gt; & lt; iframe & gt; Support Limited in HTML5 and mostly deprecated. Fully supported in HTML5 and across modern browsers. & 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?
Using the SwitchTo () .frame function
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);
Switching Frames in Selenium using Index
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
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 ();
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
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 ();
Switching back to Main Page
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 ();
Testing on Real Device Cloud with BrowserStack Automate
Conclusion
Useful Resources for Selenium
Related Guides
Automate This With SUSA
Test Your App Autonomously