Handle Frames in Selenium

Price: INR 8,999.00
Price: INR 14,999.00
Was: INR 16,999.00

First, let’s understand What is Frame/iFrame?

An iFrame is an HTML document embedded inside the current HTML document on a website.

iFrame HTML element is used to insert content from another source, into a Web page. You can change an iFrame’s content without making them reload the web page. A web page can have multiple frames. In a very simple word, Frames or Iframes can be treated as the web page inside a web page.


Selenium treats frames in the same line. That means you have to switch to a frame, to find elements present inside that frame.


How to Identify Frame?

There are many ways to identify the Frame. We will see the easiest way to identify the frame. Let’s see the step by step procedure-

  1. Right, click on the element. Check if “This Frame” option is available. If This frame option is available, it means that the element is inside a frame.

  2. View page source of the web page and check if any tag is available for ‘iframe’.

Sometimes Frame name is not available then you open you Firebug inspect the desired element. It will show the iFrame tag for the frame inside which the web element is present.


To work with the frames you need to switch to frame first. In Selenium to work with iFrames, we have different ways to handle frame depending on the need.

  1. By frame Index

  2. By frame name or ID


By Frame Index

If a page has multiple frames the first frame would be at index “0”, the second at index “1” and so on.

Once the frame is switched, all selenium subsequent calls are made to that frame. i.e the driver focus will be now on the frame.

Syntax – driver.switchTo().frame(int arg0);


Selenium throws NoSuchFrameException if the frame is not present.

In the below example our text box is inside the frame so first, we will switch on the frame then type inside the textbox.

You can execute the above code on your local machine and see the result. Now control is inside the frame, It will not perform any action outside this frame. To continue with page, First, you need to switch on page using windowID (We have discussed it in Handle Multiple Window tutorial)


By Frame Name or ID

If a frame has any name or id. You can see this in frame page source and text attached with the iFrame tag. This is simple as above example. You just need to pass the frame name or ID in place of frame index.

Syntax – driver.switchTo().frame(String name);


So it’s not the tough call to handle the frames with name or ID. We just need to replace the index with name or ID and rest code will be like above.


Handle Frames inside the Frames

Sometimes when there are multiple Frames (Frame inside a frame), we need to first switch to the parent frame and then we need to switch to the child frame. Suppose you have HTML structure like this-

Frames in Selenium


In the above HTML, you can see we have frame2 inside frame1 and web elements inside the frame2. Now let’s see how can we handle these kinds of situations in selenium.


So we have seen, How can we manage the frames and frames inside the frame.


Practice those tutorials is highly recommended. It will be good if parallelly you learn Java as well.

0 Comment

Leave a Reply