Iframes and frames in Selenium

If you need to work with frames or iframes, Consider a button within an iframe given above image. If we inspect the element using the browser development tools,

//Below line code won't work
driver.findElement(By.tagName("button")).click();

Using a WebElement

Switching using a WebElement is the most flexible option. You can find the frame using your preferred selector and switch to it.

//Store the web element
WebElement iframe = driver.findElement(By.id("buttonframe"));

//Switch to the frame
driver.switchTo().frame(iframe);

//Now we can click the button
driver.findElement(By.tagName("button")).click();

Using a name or ID
If your frame or iframe has an id or name attribute, this can be used instead. If the name or ID is not unique on the page, then the first one found will be switched to.

//Using the ID
driver.switchTo().frame("buttonframe");

//Or using the name instead
driver.switchTo().frame("myframe");

//Now we can click the button
driver.findElement(By.tagName("button")).click();

Using an index 
It is also possible to use the index of the frame, such as can be queried using window.frames in JavaScript.

// Switches to the second frame
driver.switchTo().frame(1);

Leaving a frame
To leave an iframe or frameset, switch back to the default content like so:

// Return to the top level
driver.switchTo().defaultContent();

Frames in Selenium :

1. Selenium provides multiple options to switch the context to iFrame such as index, id, name, and web element.

2. The driver.switchTo.frame() method is used to switch the context to iFrame.

3. You can get the total count of iFrame either by using JavaScriptExecutor or the findElementsbyTagName() method.

4. When there are no unique attributes associated with iFrame, you can use the frame index switch to the frame using the frame index.

5. The driver.switchTo().parentFrame();  is used for switching the context to the parent frame.

6. driver.switchTo().defaultContent();  is used for switching context to topmost page.

7. Automating nested iFrame requires switching the context to a different iFrame.

How to Handle the iFrames in Selenium?

Selenium provides different techniques to automate all possible scenarios for handling iframe. Lets discuss how to handle iframe in selenium below.

selenium provides a special command. driver.switchTo().frame()

The driver.switchTo().frame() helps to identify and perform the actions on elements inside the iFrame.

Selenium provides multiple ways to switch over to iFrame.

1. ID

2. Name

3. Index

4. WebElement

Find the Total Number of Frames on the webpage
There are scenarios where you might need to get the iFrame count of the webpage. This is mostly useful when your webpage has multiple iFrames.

There are two ways to get the number of iFrames on the Webpage.

1. Get the total number of iFrames Selenium using Javascript Executor

JavascriptExecutor jse = (JavascriptExecutor) driver;

Integer numberOfFrames = Integer.parseInt(jse.executeScript(“return window.length”).toString());

System.out.println(“Number of iframes on the page are ” + numberOfFrames);

In the above code, we are using the JavascriptExecutor to get the count of iFrame inside the webpage, The window.length in JavaScript executor returns the count of iFrame. 

2. Get the count of iFrame inside the webpage using Selenium Find Elements  method

List<WebElement> iframeElements = driver.findElements(By.tagName(“iframe”));

System.out.println(“The total number of iframes are ” + iframeElements.size());

In the second method, we are using the driver.findElements() methods, which returns the list of elements having tagName iFrame.  Once you get the list, you can calculate the list size, which is the total number of iFrames present on the webpage, using the size() method i.e iframeElements.size();