Learning Path: RookieNerd Academy → Playwright with Java
Module: 9 – Handling Frames and iFrames
Difficulty: Beginner to Intermediate
Estimated Reading Time: 22 Minutes
SEO Title
Playwright Frames and iFrames in Java – Complete Guide with Examples (2026)
Meta Description
Learn how to handle frames and iframes in Playwright Java with practical examples. Master nested frames, switching between frames, interacting with elements, and best practices for reliable automation.
URL Slug
/academy/playwright/playwright-frames-iframes-java
Primary Keyword
Playwright Frames Java
Secondary Keywords
Playwright iFrame Java
Playwright FrameLocator
Playwright Java Tutorial
Playwright Nested Frames
Playwright Handle Frames
After completing this lesson, you will be able to:
Understand what Frames and iFrames are.
Work with Playwright's FrameLocator.
Handle nested frames.
Interact with elements inside frames.
Troubleshoot common frame-related issues.
Many enterprise web applications use Frames or iFrames to embed content from another page.
Common examples include:
Payment gateways
Embedded YouTube videos
Google Maps
Chat widgets
Online document viewers
Rich text editors
Third-party login components
When an element is inside an iFrame, Playwright cannot interact with it directly from the main page. You must first access the correct frame.
Understanding this concept is essential because many real-world applications depend on embedded content.
An iFrame (Inline Frame) allows one HTML document to be displayed inside another HTML document.
Example:
<iframe src="payment.html"></iframe>
Although the payment form appears on the page, it actually belongs to a separate document with its own DOM.
Suppose an online shopping application embeds its payment form inside an iFrame.
If your script tries to locate the card number field directly:
page.locator("#cardNumber").fill("4111111111111111");
the test will fail because the element is inside the frame, not on the main page.
Playwright provides the frameLocator() API to work with frames.
Example:
page.frameLocator("#payment-frame")
.locator("#cardNumber")
.fill("4111111111111111");
This tells Playwright:
Locate the payment frame.
Find the card number field inside it.
Enter the value.
page.frameLocator("#loginFrame")
.getByLabel("Username")
.fill("rookienerd");
page.frameLocator("#loginFrame")
.getByLabel("Password")
.fill("Password123");
page.frameLocator("#loginFrame")
.getByRole(AriaRole.BUTTON,
new Page.GetByRoleOptions().setName("Login"))
.click();
Some enterprise applications contain frames inside other frames.
Structure:
Main Page
│
├── Frame A
│ │
│ └── Frame B
│ │
│ └── Login Button
Playwright allows chaining FrameLocators.
Example:
page.frameLocator("#frameA")
.frameLocator("#frameB")
.getByRole(AriaRole.BUTTON,
new Page.GetByRoleOptions().setName("Submit"))
.click();
Assertions work inside frames as well.
Locator successMessage = page
.frameLocator("#payment-frame")
.locator(".success-message");
assertThat(successMessage)
.hasText("Payment Successful");
Imagine testing an online banking application.
Workflow:
User logs in.
Opens Fund Transfer.
Payment form loads inside an iFrame.
User enters payment details.
Confirmation message appears.
Your automation should:
Switch to the payment frame.
Fill the required fields.
Submit the form.
Verify the confirmation message.
Prefer frameLocator() over older frame handling approaches.
Use meaningful frame identifiers whenever possible.
Combine frame interactions with Playwright assertions.
Avoid fixed waits such as Thread.sleep().
Trying to locate an element directly from the main page when it actually exists inside an iFrame.
Frames often load asynchronously. Rely on Playwright's automatic waiting instead of fixed delays.
Always inspect the page structure when elements cannot be found.
Create a script that:
Opens a page containing an iFrame.
Enters text into a field inside the frame.
Clicks a button.
Verifies a success message using Playwright assertions.
What is the difference between a Frame and an iFrame?
How do you interact with elements inside an iFrame using Playwright?
What is FrameLocator?
How do you handle nested frames?
Why can't you locate an iFrame element directly from the main page?
No. You must identify the correct frame using frameLocator() or another appropriate API.
Yes. Assertions work normally once you've located the element within the frame.
Yes. You can chain FrameLocator calls to access elements inside nested frames.
In this lesson, you learned:
What Frames and iFrames are.
Why they're commonly used in modern web applications.
How to use FrameLocator.
How to work with nested frames.
Best practices for reliable frame automation.
Frames are common in enterprise applications, especially where third-party content or secure payment components are embedded. Mastering them will make your Playwright tests more robust and easier to maintain.
Playwright Browser Context
Playwright Multiple Tabs and Windows
Playwright Auto Waiting
Playwright Assertions
➡ Playwright File Upload and File Download in Java – Complete Guide
Main page containing an iFrame.
DOM structure highlighting the iFrame.
FrameLocator example in action.
Nested frame architecture diagram.
Successful payment confirmation inside an iFrame.