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.

const { chromium } = require('playwright')

;(async () => {
  const browser = await chromium.launch()
  const context = await browser.newContext()
  const page = await context.newPage()

  await page.goto('https://your-page-with-an-iframe.com')

  const header = await page.frameLocator('iframe').locator('h1')
  console.log(await header.innerText())

  await browser.close()
})()

Further reading

  1. Playwright’s “Frames documentation”

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