How to Handle Frames in Selenium

After learning how to find frames we are going to see how to handle frames in Selenium. That is after figuring out the number of frames that are there on your webpage, handling it becomes fairly easy.

These are all the methods that you could use to handle a frame in Selenium:

frame methods in selenium

Steps on How to Handle Frames in Selenium

As suggested in the image above, there are three ways you can identify a frame in Selenium. They are:

  • By index
  • By name or Id
  • Using Web Element

We are going to see each of them one by one.

meme for frames

By Index

So I am assuming you have already counted the number of frames you have and identified which frame is the one that you wish to switch to.

So websites that are not volatile, or that have frames that aren’t going to change very frequently, you can make use of their indices to directly switch to them.

First, you need to do identify the number of frames, and focus on the frame number that you wish to handle.

Let’s say the frame you wish to work on is the third frame. Since we are dealing with indices here, the frame by which the third one gets identified is 2. Since indices start from 0.

You can simply make use of the:

frame(int arg0) 

method shown in the diagram above.

The way to use it is by simply typing:

driver.switchTo().frame(2);

This will take the focus to frame 2. Now you can perform any activity you want on the frame.

Switching Back to Original Frame

Okay, let’s say your job is done and you wish to switch back to your previous frame.

How do you go back to your existing frame?

Simple. Just use the following:

driver.switchTo().parentFrame();

or you can identify the index of the original frame, and then use it as well.

Another alternative is to use default content. Here’s how to do it:

driver.switchTo().defaultContent();

It will take the control back to your previous frame, which was nothing but your parent frame.

By Name or Id

The second method that you could use to identify a frame is by using its name or Id. If you inspect the iframe that you wish to switch to, you will notice that an iframe must have an Id or a name. For e.g. amazon has one of these iframes:

The id of the iframe here is:

handle frames in selenium by name

So when we have an id or name of the frame we wish to switch to, then we can simply use the following method:

driver.switchTo().frame("ape_Gateway_right-7_desktop_iframe");

Now after switching to the frame you wish to work on, you can perform any other task you wish to on that frame.

Remember once done, you can navigate back to your original frame using:

driver.switchTo().parentFrame();

or by using its very own frame id or name.

Handle Frames By Web Element

The third way to switch to an iframe you wish to work on is using Web element.

The way to do it is by typing:

driver.switchTo().frame(WebElement);

where you need to specify the WebElement instead of name or index.

In the previous line just use the xpath of the iframe you wish to switch to, and declare it as a WebElement like this:

WebElement iframeWE = driver.findElement(By.xpath("//*[@id="ape_Gateway_right-7_desktop_iframe"]"));

then use the iframeWE web element in the following:

driver.switchTo().frame(iframeWE);

Then you can perform whatever functions you wish to on the identified iframe.

Scottshak

Poet. Author. Blogger. Screenwriter. Director. Editor. Software Engineer. Author of "Songs of a Ruin" and proud owner of four websites and two production houses. Also, one of the geekiest Test Automation Engineers based in Ahmedabad.

You may also like...

1 Response

  1. July 23, 2019

    […] Next story How to Handle Frames in Selenium […]

Leave a Reply