puppeteer/website/versioned_docs/version-18.1.0/api/puppeteer.frame.md
release-please[bot] 3aee641c3d
chore(main): release 18.1.0 (#9042)
🤖 I have created a release *beep* *boop*
---


##
[18.1.0](https://github.com/puppeteer/puppeteer/compare/v18.0.5...v18.1.0)
(2022-10-05)


### Features

* **chromium:** roll to Chromium 107.0.5296.0 (r1045629)
([#9039](https://github.com/puppeteer/puppeteer/issues/9039))
([022fbde](022fbde85e))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: release-please[bot] <55107282+release-please[bot]@users.noreply.github.com>
2022-10-05 08:27:19 +00:00

14 KiB

sidebar_label
Frame

Frame class

Represents a DOM frame.

To understand frames, you can think of frames as <iframe> elements. Just like iframes, frames can be nested, and when JavaScript is executed in a frame, the JavaScript does not effect frames inside the ambient frame the JavaScript executes in.

Signature:

export declare class Frame

Remarks

Frame lifecycles are controlled by three events that are all dispatched on the parent page:

The constructor for this class is marked as internal. Third-party code should not call the constructor directly or create subclasses that extend the Frame class.

Example 1

At any point in time, pages expose their current frame tree via the Page.mainFrame() and Frame.childFrames() methods.

Example 2

An example of dumping frame tree:

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.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 + '  ');
    }
  }
})();

Example 3

An example of getting text from an iframe element:

const frame = page.frames().find(frame => frame.name() === 'myframe');
const text = await frame.$eval('.selector', element => element.textContent);
console.log(text);

Methods

Method Modifiers Description
$(selector) Queries the frame for an element matching the given selector.
$$(selector) Queries the frame for all elements matching the given selector.
$$eval(selector, pageFunction, args)

Runs the given function on an array of elements matching the given selector in the frame.

If the given function returns a promise, then this method will wait till the promise resolves.

$eval(selector, pageFunction, args)

Runs the given function on the first element matching the given selector in the frame.

If the given function returns a promise, then this method will wait till the promise resolves.

$x(expression)
addScriptTag(options) Adds a <script> tag into the page with the desired url or content.
addStyleTag(options) Adds a <link rel="stylesheet"> tag into the page with the desired URL or a <style type="text/css"> tag with the content.
addStyleTag(options)
childFrames()
click(selector, options) Clicks the first element found that matches selector.
content()
evaluate(pageFunction, args) Behaves identically to Page.evaluate() except it's run within the the context of this frame.
evaluateHandle(pageFunction, args) Behaves identically to Page.evaluateHandle() except it's run within the context of this frame.
focus(selector) Focuses the first element that matches the selector.
goto(url, options) Navigates a frame to the given url.
hover(selector) Hovers the pointer over the center of the first element that matches the selector.
isDetached()
isOOPFrame()
name()
page()
parentFrame()
select(selector, values) Selects a set of value on the first <select> element that matches the selector.
setContent(html, options) Set the content of the frame.
tap(selector) Taps the first element that matches the selector.
title()
type(selector, text, options) Sends a keydown, keypress/input, and keyup event for each character in the text.
url()
waitForFunction(pageFunction, options, args)
waitForNavigation(options)

Waits for the frame to navigate. It is useful for when you run code which will indirectly cause the frame to navigate.

Usage of the History API to change the URL is considered a navigation.

waitForSelector(selector, options)

Waits for an element matching the given selector to appear in the frame.

This method works across navigations.

waitForTimeout(milliseconds)
waitForXPath(xpath, options)