TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: Learn to Interact with iFrame Elements
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
Selenium Tutorial

Learn to Interact with iFrame Elements

Last updated: Sep 18, 2023 10:09 pm
By Meenakshi Agarwal
Share
6 Min Read
Learn to interact with iFrame elements
SHARE

In this Selenium tutorial, you will learn to interact with iFrame elements.

Contents
Count no. of iFrames on a web pageInteract with elements from a single iFrameInteract with elements of Nested iFramesWait for an iFrame to load
Learn to interact with iFrame elements

Interacting with iFrame Elements

An iFrame is a unique HTML entity that opens a web page inside another page. Once you get the focus on the iFrame, you might need to work with its elements.

Since it is a page within another page, you need first to select the target iFrame and then access its elements via standard Selenium locator strategies. Let’s try to understand all this with the help of real-time examples.

In the first example, you will find code and explanation to interact with one iFrame and iFrame elements. The second one is addressing when the web page has multiple iFrames.

To learn more check out – Handle iFrame in Selenium

Before you start working with either single or nested iFrames on a web page, you should first search for their presence.

Count no. of iFrames on a web page

Following is the Selenium command to help you locate iFrame:

List<WebElement> web_iframe = driver.findElements(By.tagName("iframe"));

Next, you can count the number of iFrames available on the web page. Check out below the code:

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class HandlingIframes {
   public static void main(String[] args) {

      // set the geckodriver.exe property
      System.setProperty("webdriver.gecko.driver", "geckodriver.exe");
      WebDriver browser = new Firefoxbrowser();

      // Open the web page url
      browser.get("web page url having iframes");

      //Searching for iframes
      List web_iframe = browser.findweb_iframe(By.tagName("iframe"));

      int iFrameCount = web_iframe.size();

      System.out.println("No. of Iframes: " + iFrameCount);
      browser.quit();
   }
}

In the above test code, the very first step is to create a Firefox WebDriver object. After object creation, we can refer to it as a browser.

Using this browser object, you can provide the link and open the web page.

After this, we are locating the list of iFrames available on the web page. The following syntax is used:

List web_iframe = browser.findweb_iframe(By.tagName("iframe"));

In this line, we are using the tagName property to find an iFrame tag. After that, we are providing this as an input to the findElements method. It returns a list of iframes as Web elements.

Finally, we used the size() of the List interface and printed the number of iFrames found on the web page onto the console.

Interact with elements from a single iFrame

If the web page has a single iFrame, then we first switch to it. After that, we find the target element and perform the desired operation.

Check out the below code:

public class SingleIFrame {
   public static void main(String[] args) throws Exception {
      // Set the geckodriver.exe property
      System.setProperty("webdriver.gecko.driver", "geckodriver.exe");
      // Launch firefox
      WebDriver browser = new FirefoxDriver();
      browser.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      // Navigate to the webpage
      browser.get("ulr containing iframes");
      // Set the text value
      String strText = "Some Demo Data";
      // Switch to iframe
      browser.switchTo().frame("my_iframe");
      // Assign the text value to target iframe text element
      browser.findElement(By.xpath("//input[@type='text']")).sendKeys(strText);
  }
}

Interact with elements of Nested iFrames

In some situations, we have multiple and nested iFrames on a web page. If an iFrame has another iFrame, then we call it a nested iFrame. In such a case, we need to switch to iFrame within the iFrame.

The content of a nested iFrame is only available from inside an iFrame. We can’t access elements from outside the iFrame.

In our example, we are checking for a checkbox that is present in a nested iFrame.

Below is the fully working code that you can utilize while working on a nested iFrame assignment.

public class NestediFrame {
   public static void main(String[] args) throws Exception {
      // Set the geckodriver.exe property
      System.setProperty("webdriver.gecko.driver", "geckodriver.exe");
      // Launch firefox
      WebDriver browser = new FirefoxDriver();
      browser.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      // Navigate to the webpage
      browser.get("ulr containing iframes");
      // Find the outer iframe
      WebElement outer_iframe = browser.findElement(By.id("outer_iframe"));
      // Switch to outer iframe
      browser.switchTo().frame(outer_iframe);
      // Locate the nested iframe
      WebElement nested_iframe = browser.findElement(By.xpath("//iframe[@id='nested_iframe']"));
      // Switch to nested iframe
      browser.switchTo().frame(nested_iframe);
      // Locate the checkbox and select the option
      WebElement option = browser.findElement(By.xpath("//input[@type='checkbox']"));
      // if option is not selected then click the checkbox
      if( !option.isSelected() ){
         option.click();
      }
   }
}

Wait for an iFrame to load

Sometimes you face a problem when switching to a specific iFrame fails because it hasn’t loaded. You can address such a case using the following webdriver-backed selenium code:

WebDriverWait waitX = new WebDriverWait(browser, 15);
  waitX.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.Id("target_frame"));

The above statement ensures that you get to the desired iFrame without any error. Simultaneously, you should be checking while accessing elements inside an iFrame.

WebDriverWait waitY = new WebDriverWait(browser, 15);
  waitY.until(ExpectedConditions.ElementIsVisible(By.Id("target_element"));

You Might Also Like

20 Demo Sites for Automation Testing

Page Object Model (POM) and Page Factory Guide in Selenium Java

Selenium 4 Relative Locators Guide

Selenium Version 4 Features – What’s New?

How to Inspect Element in Safari, Android, and iPhone

Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article How to Handle iFrame in Selenium Handle iFrame/iFrames in Selenium Webdriver
Next Article Replace all or specified no. of occurrences of chars sub strings in Python Replace All or Some Occurrences of Chars/Sub Strings

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x