How to Switch Over the Elements in iframes using Web Driver Commands

After identifying the iframe if we need to handle a web element that is present on the iframe, in such cases, we need to change the focus of selenium from the main page to the iframe. There are three different ways to shift the focus of selenium from the main page to the iframe.

1. With the help of the index 

Syntax:

driver.switchTo().frame(index);
the index always starts from zero (0,1,2,3,…..)

2. With the help of id/name

Syntax:

driver.switchTo().frame(“idvalue/namevalue”);
here we need to identify the id value or name value from the HTML codes of the iframe.

3. With the help of web element

Syntax:

driver.switchTo().frame(webelement);
here we need to declare a webelement by using attributes of iframe.

How to Handle iframe in Selenium with Java?

In this article, we are going to discuss how to handle the iframe in Selenium with Java. The following 6 points will be discussed.

Table of Content

  • What are iframes in Selenium?
  • Difference between frame and iframe in Selenium
  • Steps to identify a Frame on a Page?
  • How to Switch Over the Elements in iframes using Web Driver Commands
  • How to Switch Back to the Main iframe

Let’s start discussing each of these topics in detail.

Similar Reads

What are iframes in Selenium?

Displaying a webpage as a part of another webpage this parameter we called iframe. If the element is present on the main page then we can directly interact and we can perform the actions but when the element is present on iframe we can not directly interact with those elements. In such cases, we need to change the focus of selenium from the main page to the iframe....

Difference between frame and iframe in Selenium

Normally frames are used for the horizontally or vertically splitting of a webpage. So with the help of a frame, we can split a webpage into multiple sections. frames are the part of the page. Whereas iframes are generally used to insert the contents from other sources. Iframe sets the URL of the website to be displayed as a part of another webpage, and the width and height of the iframe can be set to determine how much of the website should be displayed....

Steps to Identify a Frame on a Page?

There are some methods from which we can identify if there is any iframe present on the webpage or not....

How to Switch Over the Elements in iframes using Web Driver Commands

After identifying the iframe if we need to handle a web element that is present on the iframe, in such cases, we need to change the focus of selenium from the main page to the iframe. There are three different ways to shift the focus of selenium from the main page to the iframe....

How to Switch Back to the Main iframe

Suppose we need to change the focus of selenium from iframe to the main page or parent iframe for which there are two different ways....