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.
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.
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.
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
WebDriver driver = new ChromeDriver();
driver.get("http://localhost:8080/demo/frames/");
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();
IWebDriver driver = new ChromeDriver();
driver.Url = "http://localhost:8080/demo/frames/";
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();
driver = webdriver.Chrome()
driver.get("http://localhost:8080/demo/frames/")
driver.implicitly_wait(30)
driver.maximize_window()
# Find the frame element
frameElement = driver.find_element(By.XPATH, "//div[@class='contentTextHeader' and contains(text(),'Example 1')]/following-sibling::iframe")
# Switch to the frame
driver.switch_to.frame(frameElement)
# Get first frame text
frame1Div1Text = driver.find_element(By.ID, "frame1div1").text
print(frame1Div1Text)
# Find the inner frame element
innerFrameElement = driver.find_element(By.XPATH, "//div[@id='frame1div1' and contains(text(),'Frame1 Div 1')]/following-sibling::iframe")
# Switch to the inner frame
driver.switch_to.frame(innerFrameElement)
# Get inner frame text
frame1InnerFrameText = driver.find_element(By.ID, "frame1Adiv1").text
print(frame1InnerFrameText)
# Switch to parent frame
driver.switch_to.parent_frame()
# Get parent frame text
parentFrameText = driver.find_element(By.ID, "frame1div1").text
print(parentFrameText)
# Switching to Default content
driver.switch_to.default_content()
driver.quit()
let driver = await new Builder().forBrowser('chrome').build();
await driver.get("http://localhost:8080/demo/frame/");
await driver.manage().setTimeouts({ implicit: 30000 });
await driver.manage().window().maximize();
// Find the frame element
let frameElement = await driver.findElement(By.xpath("//div[@class='contentTextHeader' and contains(text(),'Example 1')]/following-sibling::iframe"));
// Switch to the frame
await driver.switchTo().frame(frameElement);
//Get first frame text
let frame1Div1Text = await driver.findElement(By.id("frame1div1")).getText();
console.log(frame1Div1Text);
// Find the inner frame element
let innerFrameElement = await driver.findElement(By.xpath("//div[@id='frame1div1' and contains(text(),'Frame1 Div 1')]/following-sibling::iframe"));
// Switch to the inner frame
await driver.switchTo().frame(innerFrameElement);
//Get inner frame text
let frame1InnerFrameText = await driver.findElement(By.id("frame1Adiv1")).getText();
console.log(frame1InnerFrameText);
//Switch to parent frame
await driver.switchTo().parentFrame();
//Get parent frame text
let parentFrameText = await driver.findElement(By.id("frame1div1")).getText();
console.log(parentFrameText);
//Switching to Default content
await driver.switchTo().defaultContent();
await driver.quit();
val driver: WebDriver = ChromeDriver()
driver["http://localhost:8080/demo/frames/"]
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30))
// Find the frame element
val 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
val frame1Div1Text = driver.findElement(By.id("frame1div1")).text
println(frame1Div1Text)
// Find the inner frame element
val 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
val frame1InnerFrameText = driver.findElement(By.id("frame1Adiv1")).text
println(frame1InnerFrameText)
//Switch to parent frame
driver.switchTo().parentFrame()
//Get parent frame text
val parentFrameText = driver.findElement(By.id("frame1div1")).text
println(parentFrameText)
//Switching to Default content
driver.switchTo().defaultContent()
driver.quit()