iFrame Basics
An iframe is a separate document inside the page. Enter it first, then use the usual locators.
Page locators stop at the frame
page.getByRole(), getByText() and the others search the page's own document only. Get a locator for the frame's document, then locate inside it:
const frame = page.getByTitle('Embedded Form').contentFrame();
await frame.getByLabel('Name').fill('Ada');
await frame.getByRole('button', { name: 'Submit' }).click();
await expect(frame.getByRole('status')).toHaveText('Hello, Ada');
await expect(page.getByText('Hello, Ada')).toHaveCount(0); // not in the pagecontentFrame() or frameLocator()?
They return the same thing, a FrameLocator. contentFrame() starts from any locator for the <iframe> element, so you can find the frame by its title, role or surroundings. frameLocator() takes a CSS selector:
page.getByTitle('Newsletter signup').contentFrame();
page.frameLocator('iframe[title="Newsletter signup"]'); // same frameThe element and the document are different things
Attributes such as src and title belong to the <iframe> element in the page. Assert them on the element. owner() takes you from a frame back to its element:
const newsletter = page.frameLocator('iframe[title="Newsletter signup"]');
await expect(newsletter.owner()).toHaveAttribute('src', '/embeds/newsletter/');Embedded form
This form is an inline document (srcdoc) inside an iframe.
Newsletter
This frame loads its own page by URL, like most third-party widgets.
Related exercises
- FramesIntermediateNested & Multiple FramesReach a frame inside a frame, choose between identical frames, and find a frame with no title.
- FramesAdvancedEmbedded WidgetsTest embedded widgets the way real apps use them.
- Mini ShopIntermediateFirst E2E TestWrite your first full end-to-end test: a guest finds a laptop, pays, and gets a confirmation.
Read the guide: Testing iframes with Playwright