Frame
At every point of time, page exposes its current frame tree via the page.main_frame and frame.child_frames methods.
Frame object's lifecycle is controlled by three events, dispatched on the page object:
- page.on("frameattached") - fired when the frame gets attached to the page. A Frame can be attached to the page only once.
- page.on("framenavigated") - fired when the frame commits navigation to a different URL.
- page.on("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:
- Sync
- Async
from playwright.sync_api import sync_playwright, Playwright
def run(playwright: 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)
import asyncio
from playwright.async_api import async_playwright, Playwright
async def run(playwright: 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())
Methods
add_script_tag
Added before v1.9Returns the added tag when the script's onload fires or when the script content was injected into frame.
Adds a <script> tag into the page with the desired url or content.
Usage
frame.add_script_tag()
frame.add_script_tag(**kwargs)
Arguments
-
Raw JavaScript content to be injected into frame.
-
pathUnion[str, pathlib.Path] (optional)#Path to the JavaScript file to be injected into frame. If
pathis a relative path, then it is resolved relative to the current working directory. -
Script type. Use 'module' in order to load a JavaScript ES6 module. See script for more details.
-
URL of a script to be added.
Returns
add_style_tag
Added before v1.9Returns the added tag when the stylesheet's onload fires or when the CSS content was injected into frame.
Adds a <link rel="stylesheet"> tag into the page with the desired url or a <style type="text/css"> tag with the content.
Usage
frame.add_style_tag()
frame.add_style_tag(**kwargs)
Arguments
-
Raw CSS content to be injected into frame.
-
pathUnion[str, pathlib.Path] (optional)#Path to the CSS file to be injected into frame. If
pathis a relative path, then it is resolved relative to the current working directory. -
URL of the
<link>tag.
Returns
content
Added before v1.9Gets the full HTML contents of the frame, including the doctype.
Usage
frame.content()
Returns
drag_and_drop
Added in: v1.13Usage
frame.drag_and_drop(source, target)
frame.drag_and_drop(source, target, **kwargs)
Arguments
-
A selector to search for an element to drag. If there are multiple elements satisfying the selector, the first will be used.
-
A selector to search for an element to drop onto. If there are multiple elements satisfying the selector, the first will be used.
-
Whether to bypass the actionability checks. Defaults to
false. -
no_wait_afterbool (optional)#DeprecatedThis option has no effect.
This option has no effect.
-
scroll"auto" | "none" (optional) Added in: v1.62#Controls whether Playwright scrolls the element into view before performing the action. Defaults to
"auto", which scrolls the element into view when necessary, including scrolling nested scrollable containers. When set to"none", Playwright does not scroll the element and the action fails if the element is not already in the viewport. This is useful to assert that an element is reachable by the user without additional scrolling. -
source_positionDict (optional) Added in: v1.14#Clicks on the source element at this point relative to the top-left corner of the element's padding box. If not specified, some visible point of the element is used.
-
stepsint (optional) Added in: v1.57#Defaults to 1. Sends
ninterpolatedmousemoveevents to represent travel between themousedownandmouseupof the drag. When set to 1, emits a singlemousemoveevent at the destination location. -
strictbool (optional) Added in: v1.14#When true, the call requires selector to resolve to a single element. If given selector resolves to more than one element, the call throws an exception.
-
target_positionDict (optional) Added in: v1.14#Drops on the target element at this point relative to the top-left corner of the element's padding box. If not specified, some visible point of the element is used.
-
Maximum time in milliseconds. Defaults to
30000(30 seconds). Pass0to disable timeout. The default value can be changed by using the browser_context.set_default_timeout() or page.set_default_timeout() methods. -
When set, this method only performs the actionability checks and skips the action. Defaults to
false. Useful to wait until the element is ready for the action without performing it.
Returns
evaluate
Added before v1.9Returns the return value of expression.
If the function passed to the frame.evaluate() returns a Promise, then frame.evaluate() would wait for the promise to resolve and return its value.
If the function passed to the frame.evaluate() returns a non-Serializable value, then frame.evaluate() returns undefined. Playwright also supports transferring some additional values that are not serializable by JSON: -0, NaN, Infinity, -Infinity.
Usage
- Sync
- Async
result = frame.evaluate("([x, y]) => Promise.resolve(x * y)", [7, 8])
print(result) # prints "56"
result = await frame.evaluate("([x, y]) => Promise.resolve(x * y)", [7, 8])
print(result) # prints "56"
A string can also be passed in instead of a function.
- Sync
- Async
print(frame.evaluate("1 + 2")) # prints "3"
x = 10
print(frame