Chapter 45: Frames and iframes
Automation learning lab
Playwright Automation
Self Study Guide
Start from zero. Learn JavaScript and TypeScript, automate browsers, and build reliable end-to-end tests.
Frames and iframes
An iframe embeds a separate document inside a page. Frame locators describe the frame and then locate elements within its document. A locator in the main document does not automatically search inside every iframe.
How it works
Step-by-step walkthrough
- Create an iframe
- Enter the frame with frameLocator
- Assert its heading
Example
import { test, expect } from '@playwright/test';
test('chapter 45 practice', async ({ page }) => {
await page.setContent('<iframe title="Demo" srcdoc="<h1>Inner page</h1>"></iframe>');
const frame = page.frameLocator('iframe[title="Demo"]');
await expect(frame.getByRole('heading')).toHaveText('Inner page');
});Use a stable frame selector and avoid selecting the first arbitrary frame.
✎ Your practice task
Change the iframe title and update the locator.
Knowledge check
Which API targets content inside an iframe?
🌐 Continue learning
60-chapter learning path
Start at chapter 1, or choose any topic. Your progress is tracked separately for each chapter.
JavaScript fundamentals
TypeScript fundamentals
Playwright • setup & locators
Playwright • interactions & assertions
Playwright • framework & debugging
Playwright • real-world automation
Interactive practice lab
Try the controls below and observe the corresponding Playwright locator instructions. This browser demo does not execute Node.js or Playwright Test; copy the code to your local Playwright project to automate it.
Task manager demo
Suggested automation
Actions on the demo update these examples. In your local test use a page that contains the same controls.
Download a standalone lab page, then serve it locally (for example with Python's http.server). Navigate to it in a Playwright test.