table of contents Table of contents

How to Handle iFrames in Playwright

Playwright enables us to access and interact with iframes.

Locate an iframe and its elements

To access iframe elements, locate the iframe and query the DOM elements as if you’re in the page context.

iframe-access.spec.ts
import { test } from '@playwright/test'

test('access iframe content', async ({ page }) => {
  await page.goto('https://your-page-with-an-iframe.com')
  const header = await page.frameLocator('iframe').locator('h1')
  console.log(await header.innerText())
})

Further reading

  1. Playwright’s “Frames documentation”

Last updated on December 2, 2024. You can contribute to this documentation by editing this page on Github