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: Handle iFrame/iFrames in Selenium Webdriver
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

Handle iFrame/iFrames in Selenium Webdriver

Last updated: Sep 27, 2023 12:42 am
By Meenakshi Agarwal
Share
6 Min Read
How to Handle iFrame in Selenium
SHARE

In this tutorial, you’ll get to see how to handle IFrame in Selenium Webdriver. You can learn from the Java sample code called Selenium APIs to communicate with the IFrame element.

Contents
Selenium methods to handle IFramesIndex to manage IFrameName or ID to handle IFrameWebElement to locate IFrameNavigate to the Parent of an IFrame

Introduction

An IFrame is a container that can load a web page inside another page. It is also known as inline frames. On a web page, the iframe HTML tag represents the IFrame.

The iframe tag has an src attribute along with height and width to specify the URL of another page to load. It serves many purposes, such as loading an advert, dynamic content, etc. Please note that an IFrame can also have frames inside frames.

To learn Selenium from scratch – Selenium Webdriver Tutorial

Handling IFrames in Selenium Webdriver

How to Handle iFrame in Selenium

Selenium provides multiple functions to work with iFrames. We can choose different ways to handle IFrame relating to our needs. Please check out the following ways:

Selenium methods to handle IFrames

Mainly, Selenium provides the driver.switchTo().frame() to interact with an IFrame. However, it comes with different variations based on the argument types.

Index to manage IFrame

It selects a frame based on the index value. If the web page has more than one frame, then the first frame appears at the zeroth index, the second at 1, and so on.

After the frame is set, the WebDriver sends all calls to that frame. The driver’s focus moves to the selected frame. In such a situation, any operation on the parent page will fail and throw elements not found as we switched to one of the inner frames.

This function has the following specifications.

/***
   Function:
          driver.switchTo().frame(int arg0)

   Parameter: Index (starting from 0)
   Return value: currently selected frame
   Exception value: NoSuchFrameException (The frame isn't available)
***/

See the below example using the switchTo() function to select a frame based on its id.

public void switchToIFrameByIndex(int frame_index) {
   try {
      driver.switchTo().frame(frame_index);
      System.out.println("Navigated to frame with index: " + frame_index);
   } catch (NoSuchFrameException ex) {
      System.out.println("Unable to locate frame with index: " + frame_index
            + ex.getStackTrace());
   } catch (Exception ex) {
      System.out.println("Unable to navigate to frame with index: " + frame_index
            + ex.getStackTrace());
   }
}

Name or ID to handle IFrame

Another form of the switchTo() function allows a string to pass the frame name. It locates the frames by comparing their name attributes and gives precedence to those matched by ID.

/***
   Function:
          driver.switchTo().frame(String arg0)

   Parameters: Name Or ID - Name of the frame or the ID attribute of the frame tag.
   Return value: The driver sets the focus to the given frame
   Exception value: NoSuchFrameException - If the desired frame is not available.
***/

We’ve provided a sample code snippet demonstrating the frame name usage.

public void switchToIFrameByName(String frame_name) {
   try {
         driver.switchTo().frame(frame_name);
         System.out.println("Navigated to frame having name " + frame_name);
      } catch (NoSuchFrameException ex) {
         System.out.println("Unable to find a frame having ID " + frame_name
               + ex.getStackTrace());
      } catch (Exception ex) {
         System.out.println("Unable to locate a frame having ID " + frame_name
               + ex.getStackTrace());
   }
}

WebElement to locate IFrame

This function gets us to an IFrame based on the previous value of its WebElement.

/***
   Function:
          driver.switchTo().frame(WebElement iframe)

   Parameters: iframe - The target frame element.
   Return value: The driver sets the focus to the given frame
   Exception value: NoSuchFrameException - If the target frame is not available.
                    StaleElementReferenceException - If the WebElement is stale.
***/

Find out the below example which passes a WebElement and performs the switch.

public void switchToIFrameByWebElement(WebElement web_iframe) {
   try {
      if (isElementPresent(web_iframe)) {
         driver.switchTo().frame(web_iframe);
         System.out.println("Navigated to iframe matching web element "+ web_iframe);
      } else {
         System.out.println("Unable to navigate to the desired iframe "+ web_iframe);
      }
   } catch (NoSuchFrameException ex) {
      System.out.println("Unable to find a iframe matching web element " + web_iframe + ex.getStackTrace());
   } catch (StaleElementReferenceException ex) {
      System.out.println("Web Element with " + web_iframe + "is not bind to the page document" + ex.getStackTrace());
   } catch (Exception ex) {
      System.out.println("Unable to navigate to iframe matching web element " + web_iframe + ex.getStackTrace());
   }
}

Navigate to the Parent of an IFrame

You might need to switch to the parent page of an IFrame. It is the main page which has all the child IFrames inside. After you finish working with the IFrames, then you would want to switch back to the parent page.

Learn to interact with iFrame elements

For this purpose, you can call Webdriver’s switchTo().defaultContent() method. It will take you to the main page.

// Navgiate to your test URL page
driver.get("");

// Let's find the iframe using a locator strategy
WebElement web_iframe = driver.findElement(By.id("sample_iframe"));

// Next, perform the switch by index
driver.switchTo().frame(0);

// Finish your tasks in iframe 0
// Finally, get back to the parent page
driver.switchTo().defaultContent();

// Quit from the webdriver
driver.quit();

Now, you have learned to handle iFrame and know how to switch between iFrames. The next topic is how you interact with different elements embedded in an iframe.

We’ll cover the iFrame and its element interaction in the next tutorial. Check out – How to Interact with Elements in iFrames

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 Python join() method with examples Python Join() Method
Next Article Learn to interact with iFrame elements Learn to Interact with iFrame Elements

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