Skip to main content

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:

An example of dumping frame tree:

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)

Methods​

add_script_tag​

Added before v1.9 frame.add_script_tag

Returns 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

  • content str (optional)#

    Raw JavaScript content to be injected into frame.

  • path Union[str, pathlib.Path] (optional)#

    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.

  • type str (optional)#

    Script type. Use 'module' in order to load a JavaScript ES6 module. See script for more details.

  • url str (optional)#

    URL of a script to be added.

Returns


add_style_tag​

Added before v1.9 frame.add_style_tag

Returns 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

  • content str (optional)#

    Raw CSS content to be injected into frame.

  • path Union[str, pathlib.Path] (optional)#

    Path to the CSS file to be injected into frame. If path is a relative path, then it is resolved relative to the current working directory.

  • url str (optional)#

    URL of the <link> tag.

Returns


content​

Added before v1.9 frame.content

Gets the full HTML contents of the frame, including the doctype.

Usage

frame.content()

Returns


drag_and_drop​

Added in: v1.13 frame.drag_and_drop

Usage

frame.drag_and_drop(source, target)
frame.drag_and_drop(source, target, **kwargs)

Arguments

  • source str#

    A selector to search for an element to drag. If there are multiple elements satisfying the selector, the first will be used.

  • target str#

    A selector to search for an element to drop onto. If there are multiple elements satisfying the selector, the first will be used.

  • force bool (optional)#

    Whether to bypass the actionability checks. Defaults to false.

  • no_wait_after bool (optional)#

    Deprecated

    This 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_position Dict (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.

  • steps int (optional) Added in: v1.57#

    Defaults to 1. Sends n interpolated mousemove events to represent travel between the mousedown and mouseup of the drag. When set to 1, emits a single mousemove event at the destination location.

  • strict bool (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_position Dict (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.

  • timeout float (optional)#

    Maximum time in milliseconds. Defaults to 30000 (30 seconds). Pass 0 to disable timeout. The default value can be changed by using the browser_context.set_default_timeout() or page.set_default_timeout() methods.

  • trial bool (optional)#

    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.9 frame.evaluate

Returns 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

result = 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.

print(frame.evaluate("1 + 2")) # prints "3"
x = 10
print(frame