--- id: class-frame title: "Frame" --- At every point of time, page exposes its current frame tree via the [page.mainFrame()](api/class-page.md#pagemainframe) and [frame.childFrames()](api/class-frame.md#framechildframes) methods. [Frame] object's lifecycle is controlled by three events, dispatched on the page object: * [page.on('frameattached')](api/class-page.md#pageonframeattached) - fired when the frame gets attached to the page. A Frame can be attached to the page only once. * [page.on('framenavigated')](api/class-page.md#pageonframenavigated) - fired when the frame commits navigation to a different URL. * [page.on('framedetached')](api/class-page.md#pageonframedetached) - fired when the frame gets detached from the page. A Frame can be detached from the page only once. An example of dumping frame tree: ```js const { firefox } = require('playwright'); // Or 'chromium' or 'webkit'. (async () => { const browser = await firefox.launch(); const page = await browser.newPage(); await page.goto('https://www.google.com/chrome/browser/canary.html'); dumpFrameTree(page.mainFrame(), ''); await browser.close(); function dumpFrameTree(frame, indent) { console.log(indent + frame.url()); for (const child of frame.childFrames()) { dumpFrameTree(child, indent + ' '); } } })(); ``` An example of getting text from an iframe element: ```js const frame = page.frames().find(frame => frame.name() === 'myframe'); const text = await frame.$eval('.selector', element => element.textContent); console.log(text); ``` - [frame.$(selector)](api/class-frame.md#frameselector) - [frame.$$(selector)](api/class-frame.md#frameselector-1) - [frame.$eval(selector, pageFunction[, arg])](api/class-frame.md#frameevalselector-pagefunction-arg) - [frame.$$eval(selector, pageFunction[, arg])](api/class-frame.md#frameevalselector-pagefunction-arg-1) - [frame.addScriptTag(params)](api/class-frame.md#frameaddscripttagparams) - [frame.addStyleTag(params)](api/class-frame.md#frameaddstyletagparams) - [frame.check(selector[, options])](api/class-frame.md#framecheckselector-options) - [frame.childFrames()](api/class-frame.md#framechildframes) - [frame.click(selector[, options])](api/class-frame.md#frameclickselector-options) - [frame.content()](api/class-frame.md#framecontent) - [frame.dblclick(selector[, options])](api/class-frame.md#framedblclickselector-options) - [frame.dispatchEvent(selector, type[, eventInit, options])](api/class-frame.md#framedispatcheventselector-type-eventinit-options) - [frame.evaluate(pageFunction[, arg])](api/class-frame.md#frameevaluatepagefunction-arg) - [frame.evaluateHandle(pageFunction[, arg])](api/class-frame.md#frameevaluatehandlepagefunction-arg) - [frame.fill(selector, value[, options])](api/class-frame.md#framefillselector-value-options) - [frame.focus(selector[, options])](api/class-frame.md#framefocusselector-options) - [frame.frameElement()](api/class-frame.md#frameframeelement) - [frame.getAttribute(selector, name[, options])](api/class-frame.md#framegetattributeselector-name-options) - [frame.goto(url[, options])](api/class-frame.md#framegotourl-options) - [frame.hover(selector[, options])](api/class-frame.md#framehoverselector-options) - [frame.innerHTML(selector[, options])](api/class-frame.md#frameinnerhtmlselector-options) - [frame.innerText(selector[, options])](api/class-frame.md#frameinnertextselector-options) - [frame.isDetached()](api/class-frame.md#frameisdetached) - [frame.name()](api/class-frame.md#framename) - [frame.page()](api/class-frame.md#framepage) - [frame.parentFrame()](api/class-frame.md#frameparentframe) - [frame.press(selector, key[, options])](api/class-frame.md#framepressselector-key-options) - [frame.selectOption(selector, values[, options])](api/class-frame.md#frameselectoptionselector-values-options) - [frame.setContent(html[, options])](api/class-frame.md#framesetcontenthtml-options) - [frame.setInputFiles(selector, files[, options])](api/class-frame.md#framesetinputfilesselector-files-options) - [frame.tap(selector[, options])](api/class-frame.md#frametapselector-options) - [frame.textContent(selector[, options])](api/class-frame.md#frametextcontentselector-options) - [frame.title()](api/class-frame.md#frametitle) - [frame.type(selector, text[, options])](api/class-frame.md#frametypeselector-text-options) - [frame.uncheck(selector[, options])](api/class-frame.md#frameuncheckselector-options) - [frame.url()](api/class-frame.md#frameurl) - [frame.waitForFunction(pageFunction[, arg, options])](api/class-frame.md#framewaitforfunctionpagefunction-arg-options) - [frame.waitForLoadState([state, options])](api/class-frame.md#framewaitforloadstatestate-options) - [frame.waitForNavigation([options])](api/class-frame.md#framewaitfornavigationoptions) - [frame.waitForSelector(selector[, options])](api/class-frame.md#framewaitforselectorselector-options) - [frame.waitForTimeout(timeout)](api/class-frame.md#framewaitfortimeouttimeout) ## frame.$(selector) - `selector` <[string]> A selector to query for. See [working with selectors](./selectors.md#working-with-selectors) for more details. - returns: <[Promise]<[null]|[ElementHandle]>> Returns the ElementHandle pointing to the frame element. The method finds an element matching the specified selector within the frame. See [Working with selectors](./selectors.md#working-with-selectors) for more details. If no elements match the selector, returns `null`. ## frame.$$(selector) - `selector` <[string]> A selector to query for. See [working with selectors](./selectors.md#working-with-selectors) for more details. - returns: <[Promise]<[Array]<[ElementHandle]>>> Returns the ElementHandles pointing to the frame elements. The method finds all elements matching the specified selector within the frame. See [Working with selectors](./selectors.md#working-with-selectors) for more details. If no elements match the selector, returns empty array. ## frame.$eval(selector, pageFunction[, arg]) - `selector` <[string]> A selector to query for. See [working with selectors](./selectors.md#working-with-selectors) for more details. - `pageFunction` <[function]\([Element]\)> Function to be evaluated in browser context - `arg` <[EvaluationArgument]> Optional argument to pass to `pageFunction` - returns: <[Promise]<[Serializable]>> Returns the return value of `pageFunction` The method finds an element matching the specified selector within the frame and passes it as a first argument to `pageFunction`. See [Working with selectors](./selectors.md#working-with-selectors) for more details. If no elements match the selector, the method throws an error. If `pageFunction` returns a [Promise], then `frame.$eval` would wait for the promise to resolve and return its value. Examples: ```js const searchValue = await frame.$eval('#search', el => el.value); const preloadHref = await frame.$eval('link[rel=preload]', el => el.href); const html = await frame.$eval('.main-container', (e, suffix) => e.outerHTML + suffix, 'hello'); ``` ## frame.$$eval(selector, pageFunction[, arg]) - `selector` <[string]> A selector to query for. See [working with selectors](./selectors.md#working-with-selectors) for more details. - `pageFunction` <[function]\([Array]<[Element]>\)> Function to be evaluated in browser context - `arg` <[EvaluationArgument]> Optional argument to pass to `pageFunction` - returns: <[Promise]<[Serializable]>> Returns the return value of `pageFunction` The method finds all elements matching the specified selector within the frame and passes an array of matched elements as a first argument to `pageFunction`. See [Working with selectors](./selectors.md#working-with-selectors) for more details. If `pageFunction` returns a [Promise], then `frame.$$eval` would wait for the promise to resolve and return its value. Examples: ```js const divsCounts = await frame.$$eval('div', (divs, min) => divs.length >= min, 10); ``` ## frame.addScriptTag(params) - `params` <[Object]> - `url` <[string]> URL of a script to be added. Optional. - `path` <[string]> Path to the JavaScript file to be injected into frame. If `path` is a relative path, then it is resolved relative to the current working directory. Optional. - `content` <[string]> Raw JavaScript content to be injected into frame. Optional. - `type` <[string]> Script type. Use 'module' in order to load a Javascript ES6 module. See [script](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script) for more details. Optional. - returns: <[Promise]<[ElementHandle]>> Returns the added tag when the script's onload fires or when the script content was injected into frame. Adds a `