Last Updated On
Switching between frames in Selenium can be necessary when dealing with web pages that use iframes. The methods to switch between frames allow you to interact with elements within those frames
In this tutorial, we will learn handling iFrames using Selenium Webdriver. iFrame is a HTML document embedded inside an HTML document. iFrame is defined by an <iframe></iframe> tag in HTML. With this tag, you can identify an iFrame while inspecting the HTML tree as shown below:

Below is the page that contains Frames.

1) Switching by Name or ID
If the frame or iframe has an id or name attribute, we can switch the frames using name or ID. If the name or ID is not unique on the page, then the first one found will be switched to.
//switch To IFrame using name or id
driver.findElement(By.name("iframe1-name"));
//Switch to the frame
driver.switchTo().frame(iframe);
2) Switching by WebElement
We can find the frame using any selector and switch to it.
WebElement frameElement = driver.findElement(By.id("frameId"));
driver.switchTo().frame(frameElement);
3) Switching by Index
Switching between the frames can be done by Index also.
//switch To IFrame using index
driver.switchTo().frame(0);
4) Switching back to the default content:
To leave an iframe or frameset, switch back to the default content like:
//leave frame
driver.switchTo().defaultContent();
Let us explain frame switchching with an example:-
1) Launch new Browser and open https://demoqa.com/frames
2) Switch iFrame using any of locator strategy
3) Switch back to main content
4) Switch iFrame using index
5) Close the window
The program for the above scenario is shown below:
package com.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class iFrame_Demo {
public static void main(String[] args) {
ChromeOptions options = new ChromeOptions();
WebDriver driver = new ChromeDriver(options);
driver.manage().window().maximize();
driver.get("https://demoqa.com/frames");
//Switch iFrame using any of locator strategy
WebElement iframeElement = driver.findElement(By.id("frame1"));
driver.switchTo().frame(iframeElement);
String Frame_1 = driver.findElement(By.id("sampleHeading")).getText();
System.out.println("Switch by locator:" + Frame_1);
//Switch back to the main window
driver.switchTo().defaultContent();
String mainPage = driver.findElement(By.xpath("//*[@id='framesWrapper']/h1")).getText();
System.out.println("Back to Main page :" + mainPage);
//Switch iFrame using index
driver.switchTo().frame(1);
String Frame_2 = driver.findElement(By.id("sampleHeading")).getText();
System.out.println("Switch by Index :" + Frame_2);
//quit the browser
driver.quit();
}
}
The output of the above program is

Note:-
1. Make sure to always switch back to the default content after performing actions within a frame. This is particularly important before interacting with elements that are outside of the frame.
2. If you want to switch between multiple frames, then switch to a frame and them switch back to main window and then switch to another frame.
Congratulations. We have learnt about window switching in Selenium. I hope you find this tutorial helpful. Happy Learning!!