docs/api/puppeteer.frame.md
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 affect frames inside the ambient frame the JavaScript executes in.
export declare abstract class Frame extends EventEmitter<FrameEvents>
Extends: EventEmitter<FrameEvents>
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.
At any point in time, pages expose their current frame tree via the Page.mainFrame() and Frame.childFrames() methods.
An example of dumping frame tree:
import puppeteer from 'puppeteer';
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 + ' ');
}
}
An example of getting text from an iframe element:
const frames = page.frames();
let frame = null;
for (const currentFrame of frames) {
const frameElement = await currentFrame.frameElement();
const name = await frameElement.evaluate(el => el.getAttribute('name'));
if (name === 'myframe') {
frame = currentFrame;
break;
}
}
if (frame) {
const text = await frame.$eval('.selector', element => element.textContent);
console.log(text);
} else {
console.error('Frame with name "myframe" not found.');
}
Property
</th><th>Modifiers
</th><th>Type
</th><th>Description
</th></tr></thead> <tbody><tr><td><span id="detached">detached</span>
</td><td>readonly
boolean
</td><td> </td></tr> </tbody></table>Method
</th><th>Modifiers
</th><th>Description
</th></tr></thead> <tbody><tr><td><span id="_">$(selector)</span>
</td><td> </td><td>Queries the frame for an element matching the given selector.
</td></tr> <tr><td><span id="__">$$(selector, options)</span>
</td><td> </td><td>Queries the frame for all elements matching the given selector.
</td></tr> <tr><td><span id="__eval">$$eval(selector, pageFunction, args)</span>
</td><td> </td><td>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.
</td></tr> <tr><td><span id="_eval">$eval(selector, pageFunction, args)</span>
</td><td> </td><td>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.
</td></tr> <tr><td><span id="addscripttag">addScriptTag(options)</span>
</td><td> </td><td>Adds a <script> tag into the page with the desired url or content.
<span id="addstyletag">addStyleTag(options)</span>
</td><td> </td><td>Adds a HTMLStyleElement into the frame with the desired URL
<span id="addstyletag">addStyleTag(options)</span>
</td><td> </td><td>Adds a HTMLLinkElement into the frame with the desired URL
<span id="childframes">childFrames()</span>
</td><td> </td><td>An array of child frames.
</td></tr> <tr><td><span id="click">click(selector, options)</span>
</td><td> </td><td>Clicks the first element found that matches selector.
Remarks:
If click() triggers a navigation event and there's a separate page.waitForNavigation() promise to be resolved, you may end up with a race condition that yields unexpected results. The correct pattern for click and wait for navigation is the following:
const [response] = await Promise.all([
page.waitForNavigation(waitOptions),
frame.click(selector, clickOptions),
]);
<span id="content">content()</span>
</td><td> </td><td>The full HTML contents of the frame, including the DOCTYPE.
</td></tr> <tr><td><span id="evaluate">evaluate(pageFunction, args)</span>
</td><td> </td><td>Behaves identically to Page.evaluate() except it's run within the context of this frame.
See Page.evaluate() for details.
</td></tr> <tr><td><span id="evaluatehandle">evaluateHandle(pageFunction, args)</span>
</td><td> </td><td>Behaves identically to Page.evaluateHandle() except it's run within the context of this frame.
See Page.evaluateHandle() for details.
</td></tr> <tr><td><span id="focus">focus(selector)</span>
</td><td> </td><td>Focuses the first element that matches the selector.
<span id="frameelement">frameElement()</span>
</td><td> </td><td> </td></tr> <tr><td><span id="goto">goto(url, options)</span>
</td><td> </td><td>Navigates the frame or page to the given url.
Remarks:
Navigation to about:blank or navigation to the same URL with a different hash will succeed and return null.
:::warning
Headless shell mode doesn't support navigation to a PDF document. See the upstream issue.
:::
In headless shell, this method will not throw an error when any valid HTTP status code is returned by the remote server, including 404 "Not Found" and 500 "Internal Server Error". The status code for such responses can be retrieved by calling HTTPResponse.status().
</td></tr> <tr><td><span id="hover">hover(selector)</span>
</td><td> </td><td>Hovers the pointer over the center of the first element that matches the selector.
<span id="isdetached">isDetached()</span>
</td><td>deprecated
Istrue if the frame has been detached. Otherwise, false.
Deprecated:
Use the detached getter.
<span id="locator">locator(selector)</span>
</td><td> </td><td>Creates a locator for the provided selector. See Locator for details and supported actions.
</td></tr> <tr><td><span id="locator">locator(func)</span>
</td><td> </td><td>Creates a locator for the provided function. See Locator for details and supported actions.
</td></tr> <tr><td><span id="name">name()</span>
</td><td>deprecated
The frame's name attribute as specified in the tag.
Deprecated:
Use
const element = await frame.frameElement();
const nameOrId = await element.evaluate(frame => frame.name ?? frame.id);
Remarks:
This value is calculated once when the frame is created, and will not update if the attribute is changed later.
</td></tr> <tr><td><span id="page">page()</span>
</td><td> </td><td>The page associated with the frame.
</td></tr> <tr><td><span id="parentframe">parentFrame()</span>
</td><td> </td><td>The parent frame, if any. Detached and main frames return null.
<span id="select">select(selector, values)</span>
</td><td> </td><td>Selects a set of value on the first <select> element that matches the selector.
<span id="setcontent">setContent(html, options)</span>
</td><td> </td><td>Set the content of the frame.
</td></tr> <tr><td><span id="tap">tap(selector)</span>
</td><td> </td><td>Taps the first element that matches the selector.
<span id="title">title()</span>
</td><td> </td><td>The frame's title.
</td></tr> <tr><td><span id="type">type(selector, text, options)</span>
</td><td> </td><td>Sends a keydown, keypress/input, and keyup event for each character in the text.
Remarks:
To press a special key, like Control or ArrowDown, use Keyboard.press().
<span id="url">url()</span>
</td><td> </td><td>The frame's URL.
</td></tr> <tr><td><span id="waitforfunction">waitForFunction(pageFunction, options, args)</span>
</td><td> </td><td> </td></tr> <tr><td><span id="waitfornavigation">waitForNavigation(options)</span>
</td><td> </td><td>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.
</td></tr> <tr><td><span id="waitforselector">waitForSelector(selector, options)</span>
</td><td> </td><td>Waits for an element matching the given selector to appear in the frame.
This method works across navigations.
</td></tr> </tbody></table>