Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

Handling Frames/IFrames In Selenium WebDriver : Part 2

Posted on 03/21/2025 By admin

Hello Folks,

We have seen basic of Frames/IFrames in last post.

Before I start “Handling of frames in selenium”, I want to give a scenario which will help us in this topic.

Suppose there is a flat of 3 rooms. You can go to any room through hall. Each rooms are not inter connected means you can not go room to room directly.

  • To enter in Room 1, you need to enter via Room 1 gate.
  • To enter in Room 2, you need to enter via Room 2 gate.
  • To enter in Room 3, you need to enter via Room 3 gate.
  • If you are in Room 1 and want to go in Room 2, you need to come out from Room 1 and then need to enter in Room 2 via its own gate.
  • Room 3 has another room say Inner Room which is inside Room 3.  To go to Inner Room of Room 3, you need to first enter Room 3 through its own gate and then you need to go to Inner Room of it.
  • If you are in Inner Room of Room 3 and want to go to Room 1, First you need to get out from Inner room so that you will be in Room 3. Again you need to go out from Room 3 and enter to Room 1 through its own gate.

The same logic will be used in selenium webdriver to switch to frames.

How to Switch to frame/Iframe?

Although there are differences between Frames and Iframes but selenium webdriver handle both in same way. We know that if we need to perform any action on a web element present inside any frame/iframe, we need to switch to it.

We can switch to frame/iframe using four ways:

  1. Switch to frame/iframe using its id.
  2. Switch to frame/iframe using its name.
  3. Switch to frame/iframe using its index.
  4. Switch to frame/iframe using its web element.

Switch to frame/iframe using its id/name:

Consider below html code:

#iframeCode

#FrameCode

Above fram/iframe codes have id and name. Using id and name we can switch to respective frame/iframe.

Syntax:

WebDriver driver= driver.switchTo().frame(String id);

OR

WebDriver driver= driver.switchTo().frame(String name);

Above statement returns driver focused on the given frame.

Note: Syntax is same for both frame and iframe.

Example:

Switch frame:

driver.switchTo().frame(“TopId”);

driver.switchTo().frame(“top”);

Swicth Iframe:

driver.switchTo().frame(“InnerFrameID”);

driver.switchTo().frame(“InnerFramename”);

2. Switch to frame/iframe using its index:

  • We can switch to frame/iframe using its index which is zero based.
  • Index is the position of appearance of frame/iframe in DOM.
  • We need to be careful while finding index of any frame/iframe.
  • You can consider the same indexing as we do in xpath.
  • Selecting a frame by index is equivalent to the JS expression window.frames[index] where “window” is the DOM window represented by the current context. Once the frame has been selected, all subsequent calls on the WebDriver interface are made to that frame.
  • This way you should use when none of other ways worked.

Syntax:

WebDriver driver= driver.switchTo().frame(int frameIndex);

Above statement returns driver focused on the given frame.

Note: Syntax is same for both frame and iframe.

Example:

driver.switchTo().frame(0);

3. Switch to frame/iframe using its web element:

We can switch to frame/iframe by locating it though locators.

Note: Many people understand that we need to locate any web element which is inside desired frame/iframe and then we can switch to frame using that web element. This is not true. We need to pass frame/iframe web element not any web element which is inside frame/iframe.

Syntax:

WebDriver driver= driver.switchTo().frame(WebElement frameElement);

Example:

driver.switchTo().frame(driver.findElement(By.xpath(“//iframe[@id=’InnerFrameID’]”)));

or

driver.switchTo().frame(driver.findElement(By.id(“InnerFrameID”));

Above statement returns driver focused on the given frame.

Note: Syntax is same for both frame and iframe.

Switching back to parent frame or default content:

When we are inside a frame/iframe and want to go to other frame or go out of frame, we need to use two methods based on requirement. We can not use swicthTo().frame() way to switch back.

  1. defaultContent() method
  2. parentFrame() method

defaultContent():

  • It selects either the first frame on the page, or the main document when a page contains iframes.
  • It returns driver focused on the parent frame.

Syntax: WebDriver driver= driver.switchTo().defaultContent();

parentFrame():

  • It change focus to the parent context. If the current context is the top level browsing context, the context remains unchanged.
  • It returns driver focused on the parent frame.

Syntax: WebDriver driver= driver.switchTo().parentFrame();

Many people confuse with above methods so I will take a pictorial example to explain:

I hope above image will clear your doubt.

We will continue the same topic in next post.

If you have any doubt, feel free to ask.

If you like my posts, please like, comment, share and subscribe.

#ThanksForReading

#HappySelenium

Uncategorized

Post navigation

Previous Post: git reset
Next Post: noSuchElementException

Related Posts

Manual Testing | Make Selenium Easy Uncategorized
Java Programs 14: Java Program to Print Fibonacci Series of Given Length Using Recursion Uncategorized
Frequently Asked Java Program 20: Java Program to Find Missing Alphabets in Given String Uncategorized
March 31, 2018 – Make Selenium Easy Uncategorized
Git Tutorial 22 – Git Stash – Dirty To Clean Working Directory – Save Changes Temporarily Uncategorized
findElements Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark