Frames

A frame or an iframe is an HTML document embedded inside another HTML document. Frames are commonly used to split a web page into multiple independent sections, where each section can load a separate HTML document. When working with frames in Selenium, you need to switch the context to the frame you want to interact with before performing any actions

Following are the methods to interact with frames in selenium.

Switch to a Frame

Use the s

witchTo().frame()

method to switch to a frame by various identifiers, such as index, name, or WebElement. After switching to a frame all the interaction methods remains the same.

Switch Back to Default Content

When you are already switched to a frame, you can only interact with the elements inside the frame. To interact with the element outside the frame or the default html page, you need to switch back to the default content. Use switchTo().defaultContent() to switch back to the main (default) content.

Switch to Parent Frame

The method will help to switch to parent frame when the focus is inside the nested frame. If the current context is the top level frame, the context remains unchanged.

In the following example, first we are switching to a frame and printing the text inside the first frame. Then we are switching into a another frame inside first and again printing a text inside it. Finally switching back to parent frame and then default content.

Java
C#
Python
Javascript
Kotlin
Copy
//WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.get("https://www.testautomationstudio.com/demo/frame/");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
// Find the frame element
WebElement frameElement = driver.findElement(By.xpath("//div[@class='contentTextHeader' and contains(text(),'Example 1')]/following-sibling::iframe"));
// Switch to the frame
driver.switchTo().frame(frameElement);
//Get first frame text
String frame1Div1Text = driver.findElement(By.id("frame1div1")).getText();
System.out.println(frame1Div1Text);
// Find the inner frame element
WebElement innerFrameElement = driver.findElement(By.xpath("//div[@id='frame1div1' and contains(text(),'Frame1 Div 1')]/following-sibling::iframe"));
// Switch to the inner frame
driver.switchTo().frame(innerFrameElement);
//Get inner frame text
String frame1InnerFrameText = driver.findElement(By.id("frame1Adiv1")).getText();
System.out.println(frame1InnerFrameText);
//Switch to parent frame
driver.switchTo().parentFrame();
//Get parent frame text
String paretnFrammeText = driver.findElement(By.id("frame1div1")).getText();
System.out.println(paretnFrammeText);
//Switching to Default content
driver.switchTo().defaultContent();
driver.quit();
Copy
IWebDriver driver = new ChromeDriver();
driver.Url = "https://www.testautomationstudio.com/demo/frame/";
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
driver.Manage().Window.Maximize();

// Find the frame element
IWebElement frameElement = driver.FindElement(By.XPath("//div[@class='contentTextHeader' and contains(text(),'Example 1')]/following-sibling::iframe"));
// Switch to the frame
driver.SwitchTo().Frame(frameElement);
//Get first frame text
String frame1Div1Text = driver.FindElement(By.Id("frame1div1")).Text;
Console.WriteLine(frame1Div1Text);
// Find the inner frame element
IWebElement innerFrameElement = driver.FindElement(By.XPath("//div[@id='frame1div1' and contains(text(),'Frame1 Div 1')]/following-sibling::iframe"));
// Switch to the inner frame
driver.SwitchTo().Frame(innerFrameElement);
//Get inner frame text
String frame1InnerFrameText = driver.FindElement(By.Id("frame1Adiv1")).Text;
Console.WriteLine(frame1InnerFrameText);
//Switch to parent frame
driver.SwitchTo().ParentFrame();
//Get parent frame text
String paretnFrammeText = driver.FindElement(By.Id("frame1div1")).Text;
Console.WriteLine(paretnFrammeText);
//Switching to Default content
driver.SwitchTo().DefaultContent();

driver.Quit();
Python code coming soon
Javascript code coming soon
Kotlin code coming soon