8.0 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 abstract class Frame extends EventEmitter<FrameEvents>
Extends: EventEmitter<FrameEvents>
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:
import puppeteer from '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);
Properties
Property |
Modifiers |
Type |
Description |
---|---|---|---|
detached |
|
boolean |
Methods
Method |
Modifiers |
Description |
---|---|---|
Queries the frame for an element matching the given selector. | ||
Queries the frame for all elements matching the given selector. | ||
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. | ||
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. | ||
Adds a | ||
Adds a | ||
Adds a | ||
An array of child frames. | ||
Clicks the first element found that matches | ||
The full HTML contents of the frame, including the DOCTYPE. | ||
Behaves identically to Page.evaluate() except it's run within the context of this frame. | ||
Behaves identically to Page.evaluateHandle() except it's run within the context of this frame. | ||
Focuses the first element that matches the | ||
Navigates the frame to the given | ||
Hovers the pointer over the center of the first element that matches the | ||
|
Is Deprecated: Use the | |
Is | ||
Creates a locator for the provided selector. See Locator for details and supported actions. | ||
Creates a locator for the provided function. See Locator for details and supported actions. | ||
|
The frame's Deprecated: Use
| |
The page associated with the frame. | ||
The parent frame, if any. Detached and main frames return | ||
Selects a set of value on the first | ||
Set the content of the frame. | ||
Taps the first element that matches the | ||
The frame's title. | ||
Sends a | ||
The frame's URL. | ||
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. | ||
Waits for an element matching the given selector to appear in the frame. This method works across navigations. |