# class: Frame At every point of time, page exposes its current frame tree via the [`method: Page.mainFrame`] and [`method: Frame.childFrames`] methods. [Frame] object's lifecycle is controlled by three events, dispatched on the page object: * [`event: Page.frameattached`] - fired when the frame gets attached to the page. A Frame can be attached to the page only once. * [`event: Page.framenavigated`] - fired when the frame commits navigation to a different URL. * [`event: Page.framedetached`] - 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); ``` ## async method: Frame.$ * langs: - alias-python: query_selector - returns: <[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`. ### param: Frame.$.selector = %%-query-selector-%% ## async method: Frame.$$ * langs: - alias-python: query_selector_all - returns: <[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. ### param: Frame.$$.selector = %%-query-selector-%% ## async method: Frame.$eval * langs: - alias-python: eval_on_selector - returns: <[Serializable]> Returns the return value of [`param: pageFunction`] The method finds an element matching the specified selector within the frame and passes it as a first argument to [`param: 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 [`param: 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'); ``` ### param: Frame.$eval.selector = %%-query-selector-%% ### param: Frame.$eval.pageFunction - `pageFunction` <[function]\([Element]\)> Function to be evaluated in browser context ### param: Frame.$eval.arg - `arg` <[EvaluationArgument]> Optional argument to pass to [`param: pageFunction`] ## async method: Frame.$$eval * langs: - alias-python: eval_on_selector_all - returns: <[Serializable]> Returns the return value of [`param: 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 [`param: pageFunction`]. See [Working with selectors](./selectors.md#working-with-selectors) for more details. If [`param: 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); ``` ### param: Frame.$$eval.selector = %%-query-selector-%% ### param: Frame.$$eval.pageFunction - `pageFunction` <[function]\([Array]<[Element]>\)> Function to be evaluated in browser context ### param: Frame.$$eval.arg - `arg` <[EvaluationArgument]> Optional argument to pass to [`param: pageFunction`] ## async method: Frame.addScriptTag - returns: <[ElementHandle]> Returns the added tag when the script's onload fires or when the script content was injected into frame. Adds a `