Chaining, choosing and finding frames

Nested frames: one step at a time

Each contentFrame() enters one level. A locator from the outer frame cannot see into the inner one:

const store = page.getByTitle('Store').contentFrame();
const reviews = store.getByTitle('Reviews').contentFrame();

await reviews.getByLabel('Your review').fill('Works great');
await reviews.getByRole('button', { name: 'Post review' }).click();

await expect(store.getByText('3 reviews')).toBeVisible(); // the outer frame updated

Two frames with the same title

Both payment widgets are titled "Payment", so page.getByTitle('Payment') matches two elements and fails in strict mode. Pick one by its surroundings, the way a user would:

const pro = page
  .getByRole('region', { name: 'Pro plan' })
  .getByTitle('Payment')
  .contentFrame();

.first() and .nth() also work, but break as soon as the order changes.

The Frame API: page.frame() and page.frames()

Besides frame locators there is the older Frame object. Find one by its name attribute or URL. It does not auto-wait for the frame to exist, so use it for frames that are already loaded:

const pro = page.frame({ name: 'pay-pro' });
const terms = page.frame({ url: /\/embeds\/terms\// });

console.log(page.frames().map((frame) => frame.url())); // the page and every frame

A frame with no title

The terms frame has neither a title nor a name. Fall back to an attribute it does have:

const terms = page.locator('iframe[src="/embeds/terms/"]').contentFrame();
An iframe without a title is also an accessibility problem: screen readers announce it as just "frame". Worth reporting.

Nested frame

The store frame embeds a reviews frame. Posting a review updates the count in the store.

Choose a plan

Basic plan

$5 per month

Pro plan

$15 per month

Terms