Navigate to https://www.qafeast.com/demo, and click the frame tab.
The iframe in HTML stands for Inline Frame it is a container that loads a web page inside another page. The selenium webdriver cannot interact with the HTML elements inside the iframe, and needs to switch over to IFrame to control the HTML element inside the Iframe.
There are various ways to switch over Iframe,
- Switch to iframe by index
- Switch to iframe by name/id of the frame
- Switch to iframe by webelement
- Switch over to all the iframes
Press F12 on the keyboard and inspect iframe, there are iframes , to get the text from the iframe "Right" we need to switch over to the iframe. We can switch to iframe by the above methods.

Switch to the iframe by index
driver.switchTo().frame(2);
String getBodyText = driver.findElement(By.xpath("//body")).getText();
System.out.println(getBodyText);
// Switch to Parent frame
driver.switchTo().defaultContent();
Switch to iframe by name/id of the frame
driver.switchTo().frame("frame-bottom");
// Get the text
String getBodyText = driver.findElement(By.xpath("//body")).getText();
System.out.println(getBodyText);
// Switch to Parent frame
driver.switchTo().defaultContent();
Switch to iframe by webelement
We can switch over the iframe using webelement, first we need to find the element of the iframe and then switch to the iframe
WebElement ele=driver.findElement(By.xpath("//iframe[@name='frame-bottom']"));
driver.switchTo().frame(ele);
String getBodyText = driver.findElement(By.xpath("//body")).getText();
System.out.println(getBodyText);
// Switch to Parent frame
driver.switchTo().defaultContent();
Switch over to all the iframes
We can switch over to all the iframes by fetching using findElements().
// Get all the iframes
List framesList = driver.findElements(By.xpath("//iframe"));
System.out.println(framesList.size());
for(WebElement frame:framesList){
//Switch over to Iframe
driver.switchTo().frame(frame);
//Code snippet do something with iframe
}
driver.switchTo().defaultContent();
Note: Once we switch over to iframe we need to switch back to the parent Iframe otherwise we cannot interact with the parent iframe.