Software Testing Learning Hub

Identifying and Handling the Frames with Selenium Webdriver

Many Webpages usually has frames embedded in it. Selenium provides the unique way to identify and handle the frames. Html Frames or Iframes helps to represent the content in different sections of the webpage and also helps in processing certain section of page, instead of reloading the entire page again and again.

we can identify the frame using following tags

  • By ID
  • By Name
  • By Index

lets take an example and then we will write a code for it.

lets say i have a page with three frames in it. my html code of the page will look like this…

....
<iframe id ="center", src="demo_iframe.htm" width="200" height="200"></iframe>
<iframe name ="loginwindow", src="demo_iframe.htm" width="200" height="200"></iframe>
<iframe src="demo_iframe.htm" width="200" height="200"></iframe>
.....

 

now 1st frame has ID tag, 2nd Frame has Name tag and 3rd Frame do not have any tag for identification.

lets write a code to identify the frames and switch to frame and come back to main html page.

public void handlingIframes()
{
driver.get ("url name");
// Switch to 1st frame.frame method will idenfity the frame using id attribute provided.
driver.switchTo().frame("center");
// now you can perform any operation on this frame. write code here.
// switch back to parent html page.
driver.switchTo().defaultContent();
// Switch to 2nd frame.frame method will idenfity the frame using name attribute provided.
driver.switchTo().frame("loginwindow");
// now you can perform any operation on this frame. write code here.
// switch back to parent html page.
driver.switchTo().defaultContent();
// Switch to 3rd frame.frame method will idenfity the frame using Index provided.
driver.switchTo().frame(2);
// now you can perform any operation on this frame. write code here.
// switch back to parent html page.
driver.switchTo().defaultContent();
}