Handling iFrames / Frames using Selenium Webdriver

Frame : Frame is a HTML tag that is used to divide the web page into various frames/windows. Used as <frame> tag, it specifies each frame within a frameset tag

IFrame : Iframe as <iframe> is also a tag used in HTML but it specifies an inline frame which means it is used to embed some other document within the current HTML document.




Programs to handle frames in selenium

Syntax 1

If you know the total number of frames in the web page then using the index, you can easily switch.

The index generally starts with zero so if you have only one frame then the index will be zero. If you don’t know the total number of frames in the page then you can use findElementBytagname method
driver.switchTo().frame(indexnumber);
Syntax 2

If you know the name of frames in web page then using name also, you can easily switch
driver.switchTo().frame(“framename”);
Syntax 3

If you want to identify the object using webelement we can use belwo syntax
WebElement button=driver.findElement(By.xpath(""));
driver.switchTo().frame(button);
In the same way if you want to perform any activity in frame inside the frame,  need to switch to child  frame by using one of the syntax above.
If user wants to switch back to parent or default frame below is the sytax
  • driver.switchTo().parentFrame();
  • driver.switchTo().defaultContent();