# 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 + ' '); } } })(); ``` ```python async import asyncio from playwright.async_api import async_playwright async def run(playwright): firefox = playwright.firefox browser = await firefox.launch() page = await browser.new_page() await page.goto("https://www.theverge.com") dump_frame_tree(page.main_frame, "") await browser.close() def dump_frame_tree(frame, indent): print(indent + frame.name + '@' + frame.url) for child in frame.child_frames: dump_frame_tree(child, indent + " ") async def main(): async with async_playwright() as playwright: await run(playwright) asyncio.run(main()) ``` ```python sync from playwright.sync_api import sync_playwright def run(playwright): firefox = playwright.firefox browser = firefox.launch() page = browser.new_page() page.goto("https://www.theverge.com") dump_frame_tree(page.main_frame, "") browser.close() def dump_frame_tree(frame, indent): print(indent + frame.name + '@' + frame.url) for child in frame.child_frames: dump_frame_tree(child, indent + " ") with sync_playwright() as playwright: run(playwright) ``` ## 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) 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) 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) 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'); ``` ```python async search_value = await frame.eval_on_selector("#search", "el => el.value") preload_href = await frame.eval_on_selector("link[rel=preload]", "el => el.href") html = await frame.eval_on_selector(".main-container", "(e, suffix) => e.outerHTML + suffix", "hello") ``` ```python sync search_value = frame.eval_on_selector("#search", "el => el.value") preload_href = frame.eval_on_selector("link[rel=preload]", "el => el.href") html = frame.eval_on_selector(".main-container", "(e, suffix) => e.outerHTML + suffix", "hello") ``` ### param: Frame.$eval.selector = %%-query-selector-%% ### param: Frame.$eval.pageFunction * langs: js - `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) 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); ``` ```python async divs_counts = await frame.eval_on_selector_all("div", "(divs, min) => divs.length >= min", 10) ``` ```python sync divs_counts = frame.eval_on_selector_all("div", "(divs, min) => divs.length >= min", 10) ``` ### param: Frame.$$eval.selector = %%-query-selector-%% ### param: Frame.$$eval.pageFunction * langs: js - `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 `