Introduction

Frames (also called iFrames) allow one HTML page to be embedded inside another webpage. Since elements inside a frame belong to a different DOM, Selenium cannot interact with them directly.

Before performing any action inside a frame, Selenium must switch the driver's focus to that frame.

Selenium provides multiple ways to switch into frames:

Advertisement
  • By index
  • By name or ID
  • By WebElement
  • By nested frame hierarchy
  • Back to the main page using defaultContent()

This tutorial contains practical exercises ranging from beginner to advanced real-world scenarios.


Frame Switching Methods

Selenium supports the following methods for switching between frames.

Switch by Index

Useful when the page contains only a few static frames.

 
driver.switchTo().frame(0);
 

Switch by Name or ID

Preferred when the frame has a stable name or ID attribute.

 
driver.switchTo().frame("singleframe");
 

Switch by WebElement

Useful when frame indexes change dynamically.

 
WebElement frame = driver.findElement(By.id("singleframe"));

driver.switchTo().frame(frame);
 

Handle Nested Frames

Switch to the parent frame first, followed by the child frame.

 
driver.switchTo().frame(parentFrame);

driver.switchTo().frame(childFrame);
 

Switch Back to Main Page

After completing actions inside a frame, switch back to the main page.

 
driver.switchTo().defaultContent();
 

Level 1 – Basic Practice Exercises (0–1 Year)

Exercise 1 – Switch to a Frame Using Index

Objective

Switch to a single frame using its index.

Task

  • Open a webpage containing one frame
  • Switch using frame(index)
  • Enter text inside the frame
  • Return to the main page
  • Close the browser

Focus

Understanding basic frame switching.


Exercise 2 – Switch Using Name or ID

Objective

Practice switching using the frame's name or ID.

Task

  • Open the webpage
  • Switch using name or ID
  • Click a button or enter text
  • Return to the main page

Focus

Most commonly used in real-world applications.


Exercise 3 – Switch Using WebElement

Objective

Locate the frame as a WebElement.

Task

  • Locate the frame
  • Switch using the WebElement
  • Perform actions inside the frame
  • Return to the main page

Focus

Useful for dynamic web applications.


Exercise 4 – Handle Nested Frames

Objective

Practice navigating nested frames.

Task

  • Switch to outer frame
  • Switch to inner frame
  • Perform an action
  • Return to outer frame
  • Return to the main page

Focus

Handling multiple frame levels correctly.


Exercise 5 – Return to Default Content

Objective

Switch back to the main page after working inside a frame.

Task

  • Switch to frame
  • Perform an action
  • Call defaultContent()
  • Perform another action on the main page
  • Verify successful interaction

Focus

Avoiding element interaction errors.


Level 2 – Intermediate Practice Exercises (1–3 Years)

Exercise 6 – Multi-Frame Interaction

Objective

Work with multiple frames within one test.

Task

Focus

Real project workflows.


Exercise 7 – Validate Elements Inside and Outside Frames

Objective

Verify application behavior across frames.

Task

  • Switch to frame
  • Read text
  • Validate content
  • Return to main page
  • Validate another element

Focus

End-to-end verification.


Exercise 8 – Fill a Form Inside Nested Frames

Objective

Automate a complete form inside nested frames.

Task

  • Switch to outer frame
  • Switch to inner frame
  • Fill the form
  • Submit
  • Return to the main page
  • Verify success message

Focus

Login and checkout workflows.


Exercise 9 – Dynamic Frame Switching

Objective

Handle dynamically changing frames.

Task

  • Locate frame dynamically
  • Switch using WebElement or name
  • Perform actions
  • Validate successful interaction

Focus

Avoid relying on frame indexes.


Exercise 10 – Scroll Inside a Frame

Objective

Interact with large content inside an iFrame.

Task

  • Switch to frame
  • Scroll
  • Click or enter text
  • Return to the main page

Focus

Handling large embedded pages.


Level 3 – Advanced & Scenario-Based Exercises (3–5 Years)

Exercise 11 – Nested Frames with Validations

Objective

Perform validations across multiple frame levels.

Task

  • Switch to outer frame
  • Switch to inner frame
  • Perform an action
  • Capture a screenshot
  • Return to outer frame
  • Perform another action
  • Return to default content
  • Validate page title

Focus

Reporting framework integration.


Exercise 12 – Login Workflow Inside a Frame

Objective

Automate a real login process.

Task

  • Login page inside frame
  • Enter credentials
  • Return to main page
  • Handle OTP outside frame
  • Validate successful login

Focus

Enterprise login automation.


Exercise 13 – Parallel Frame Execution

Objective

Execute frame-based tests in parallel.

Task

  • Switch to different frames
  • Execute tests simultaneously
  • Ensure each thread handles only its own frame

Focus

Thread-safe Selenium automation.


Exercise 14 – Dynamic Advertisement iFrames

Objective

Handle dynamically loaded advertisement frames.

Task

  • Detect popup frame
  • Switch into it
  • Close advertisement
  • Return to main page
  • Continue test execution

Focus

E-commerce automation.


Exercise 15 – Nested iFrame with Alert

Objective

Handle frames and alerts together.

Task

  • Switch to outer frame
  • Switch to inner frame
  • Trigger alert
  • Accept alert
  • Return to main page
  • Verify application functionality

Focus

Complex real-world Selenium scenarios.


Real-Time Project Interview Questions

1. How did you locate frames when their indexes changed dynamically?

2. Which frame switching method do you prefer and why?

3. How do you automate nested frames?

4. How do you return to the main page after working inside a frame?

5. Have you worked with dynamic advertisement iFrames?

6. How do you validate elements inside and outside frames within one test?

7. How do you automate alerts that appear inside frames?

8. What challenges have you faced while handling nested frames?

9. How do you automate frame interactions during parallel execution?

10. How do you design reusable frame utilities in your Selenium framework?


Practice Focus by Experience Level

0–1 Year Experience

Focus on learning:

  • Switching by index
  • Switching by name or ID
  • Switching by WebElement
  • Returning using defaultContent()

1–3 Years Experience

Focus on:

  • Nested frames
  • Dynamic frame handling
  • Element validations
  • Multi-frame workflows

3–5 Years Experience

Focus on:

  • Parallel execution
  • Dynamic popup frames
  • Alerts inside frames
  • Thread-safe automation
  • Real-world enterprise scenarios

Site-Based Practice Exercises

All beginner exercises can be practiced using the Frames page available on Automation Testing Demo.


Exercise 1 – Switch by Index

 
// Switch to first frame

driver.switchTo().frame(0);

// Enter text inside frame

driver.switchTo().defaultContent();
 

Exercise 2 – Switch by Name or ID

 
driver.switchTo().frame("singleframe");

// Enter text

driver.switchTo().defaultContent();
 

Exercise 3 – Switch by WebElement

 
WebElement frame = driver.findElement(By.xpath("//iframe[@id='singleframe']"));

driver.switchTo().frame(frame);

// Enter text inside frame

driver.switchTo().defaultContent();
 

Exercise 4 – Nested Frames

Practice Steps

  • Click Iframe within an Iframe
  • Switch to outer frame
  • Switch to inner frame
  • Enter text
  • Return to outer frame
  • Return to default content

Exercise 5 – Return to Default Content

Practice Steps

  • Switch to any frame
  • Perform an action
  • Call driver.switchTo().defaultContent()
  • Validate an element on the main page

Exercise 6 – Multi-Frame Interaction

Practice Steps

  • Switch to first frame
  • Perform action
  • Return to main page
  • Switch to second frame
  • Perform another action
  • Return to default content
  • Validate the main page

Frequently Asked Questions

What are the three ways to switch into a frame?

Selenium allows switching to frames:

  • By index
  • By name or ID
  • By WebElement

How do you exit a frame?

Use:

 
driver.switchTo().defaultContent();
 

To move back one level from a nested frame, use:

 
driver.switchTo().parentFrame();
 

How do you handle nested frames?

Switch to each frame in order.

Example

Outer Frame → Inner Frame → Perform Action → Parent Frame → Default Content


Why can't Selenium interact with elements outside the frame?

Because Selenium is still focused on the current frame.

Switch back using defaultContent() before interacting with elements on the main page.


Which website is best for practicing Selenium frames?

The Automation Testing Demo Frames page is an excellent practice website because it includes:

  • Single frames
  • Nested frames
  • Multiple frame switching scenarios