Handle frames in selenium webdriver using different approach

In some application for better visibility developer use frame concept in web pages. In this article, we will mainly deal with handle frames in selenium webdriver.

In this case, if your element exists in frames then we have to switch to frame first then we have to perform our operation.


handle frames in selenium

In Selenium we can use Switch to method to handle multiple windows, javascript alert, and frames as well.


Switch to frame in selenium webdriver

In Selenium we can switch to frames using switchTo () method then perform the action on that element



Note- You can give frame name or id or index or WebElement .


I created a video on frames which covers the same using 3 methods.


Programs to handle frames in selenium

Syntax 1-

In this scenario, 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.



We have enclosed our code with try and catch if now frame will not available this throw exception NoSuchFrameException


Syntax 2-

In this scenario, if you know the name  of frames in web page then using name also, you can easily switch



Check below screenshot that how we can identify that whether element is inside frame or not


Syntax 3-

In this scenario



Until you are in frames you can not perform any operation so once we are don with frame then switchTo parent window