Page class
Page provides methods to interact with a single tab or extension background page in the browser.
One Browser instance might have multiple Page instances.
Signature:
export declare abstract class Page extends EventEmitter<PageEvents>
Extends: EventEmitter<PageEvents>
Remarks
The constructor for this class is marked as internal. Third-party code should not call the constructor directly or create subclasses that extend the Page
class.
Example 1
This example creates a page, navigates it to a URL, and then saves a screenshot:
import puppeteer from 'puppeteer';
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
await page.screenshot({path: 'screenshot.png'});
await browser.close();
})();
The Page class extends from Puppeteer's EventEmitter class and will emit various events which are documented in the PageEvent enum.
Example 2
This example logs a message for a single page load
event:
page.once('load', () => console.log('Page loaded!'));
To unsubscribe from events use the EventEmitter.off() method:
function logRequest(interceptedRequest) {
console.log('A request was made:', interceptedRequest.url());
}
page.on('request', logRequest);
// Sometime later...
page.off('request', logRequest);
Properties
Property | Modifiers | Type | Description |
---|---|---|---|
accessibility |
| The Accessibility class provides methods for inspecting the browser's accessibility tree. The accessibility tree is used by assistive technology such as screen readers or switches. | |
coverage |
| The Coverage class provides methods to gather information about parts of JavaScript and CSS that were used by the page. | |
keyboard |
| Keyboard provides an api for managing a virtual keyboard. The high level api is Keyboard.type(), which takes raw characters and generates proper keydown, keypress/input, and keyup events on your page. | |
mouse |
| The Mouse class operates in main-frame CSS pixels relative to the top-left corner of the viewport. | |
touchscreen |
| The Touchscreen class exposes touchscreen events. | |
tracing |
| The Tracing class exposes the tracing audit interface. |
Methods
Method | Modifiers | Description |
---|---|---|
$(selector) | Runs | |
$$(selector) | The method runs | |
$$eval(selector, pageFunction, args) | This method runs | |
$eval(selector, pageFunction, args) | This method runs | |
addScriptTag(options) | Adds a | |
addStyleTag(options) | Adds a Shortcut for page.mainFrame().addStyleTag(options). | |
addStyleTag(options) | ||
authenticate(credentials) | Provide credentials for | |
bringToFront() | Brings page to front (activates tab). | |
browser() | Get the browser the page belongs to. | |
browserContext() | Get the browser context that the page belongs to. | |
click(selector, options) | This method fetches an element with | |
close(options) | ||
content() | The full HTML contents of the page, including the DOCTYPE. | |
If no URLs are specified, this method returns cookies for the current page URL. If URLs are specified, only cookies for those URLs are returned. | ||
createCDPSession() | Creates a Chrome Devtools Protocol session attached to the page. | |
createPDFStream(options) | Generates a PDF of the page with the | |
emulate(device) | Emulates a given device's metrics and user agent. To aid emulation, Puppeteer provides a list of known devices that can be via KnownDevices. | |
emulateCPUThrottling(factor) | Enables CPU throttling to emulate slow CPUs. | |
emulateIdleState(overrides) | Emulates the idle state. If no arguments set, clears idle state emulation. | |
emulateMediaFeatures(features) | ||
emulateMediaType(type) | ||
emulateNetworkConditions(networkConditions) | This does not affect WebSockets and WebRTC PeerConnections (see https://crbug.com/563644). To set the page offline, you can use Page.setOfflineMode(). A list of predefined network conditions can be used by importing PredefinedNetworkConditions. | |
emulateTimezone(timezoneId) | ||
emulateVisionDeficiency(type) | Simulates the given vision deficiency on the page. | |
evaluate(pageFunction, args) | Evaluates a function in the page's context and returns the result. If the function passed to | |
evaluateHandle(pageFunction, args) | ||
evaluateOnNewDocument(pageFunction, args) | Adds a function which would be invoked in one of the following scenarios:
The function is invoked after the document was created but before any of its scripts were run. This is useful to amend the JavaScript environment, e.g. to seed | |
exposeFunction(name, pptrFunction) | The method adds a function called If the puppeteerFunction returns a note Functions installed via | |
focus(selector) | This method fetches an element with | |
frames() | An array of all frames attached to the page. | |
getDefaultTimeout() | Maximum time in milliseconds. | |
goBack(options) | This method navigate to the previous page in history. | |
goForward(options) | This method navigate to the next page in history. | |
goto(url, options) | Navigates the page to the given | |
hover(selector) | This method fetches an element with | |
isClosed() | Indicates that the page has been closed. | |
isDragInterceptionEnabled() |
|
Deprecated: We no longer support intercepting drag payloads. Use the new drag APIs found on ElementHandle to drag (or just use the Page.mouse). |
isJavaScriptEnabled() |
| |
isServiceWorkerBypassed() |
| |
locator(selector) | Creates a locator for the provided selector. See Locator for details and supported actions. | |
locator(func) | Creates a locator for the provided function. See Locator for details and supported actions. | |
mainFrame() | The page's main frame. | |
metrics() | Object containing metrics as key/value pairs. | |
pdf(options) | Generates a PDF of the page with the | |
queryObjects(prototypeHandle) | This method iterates the JavaScript heap and finds all objects with the given prototype. | |
reload(options) | Reloads the page. | |
removeExposedFunction(name) | The method removes a previously added function via $Page.exposeFunction() called | |
removeScriptToEvaluateOnNewDocument(identifier) | Removes script that injected into page by Page.evaluateOnNewDocument. | |
screencast(options) | Captures a screencast of this page. | |
screenshot(options) | Captures a screenshot of this page. | |
screenshot(options) | ||
select(selector, values) | Triggers a | |
setBypassCSP(enabled) | Toggles bypassing page's Content-Security-Policy. | |
setBypassServiceWorker(bypass) | Toggles ignoring of service worker for each request. | |
setCacheEnabled(enabled) | Toggles ignoring cache for each request based on the enabled state. By default, caching is enabled. | |
setContent(html, options) | Set the content of the page. | |
This setting will change the default maximum navigation time for the following methods and related shortcuts: | ||
setDefaultTimeout(timeout) | ||
setDragInterception(enabled) |
| Deprecated: We no longer support intercepting drag payloads. Use the new drag APIs found on ElementHandle to drag (or just use the Page.mouse). |
setExtraHTTPHeaders(headers) | The extra HTTP headers will be sent with every request the page initiates. tip All HTTP header names are lowercased. (HTTP headers are case-insensitive, so this shouldn’t impact your server code.) note page.setExtraHTTPHeaders does not guarantee the order of headers in the outgoing requests. | |
setGeolocation(options) | Sets the page's geolocation. | |
setJavaScriptEnabled(enabled) | ||
setOfflineMode(enabled) | Sets the network connection to offline. It does not change the parameters used in Page.emulateNetworkConditions() | |
setRequestInterception(value) | Activating request interception enables HTTPRequest.abort(), HTTPRequest.continue() and HTTPRequest.respond() methods. This provides the capability to modify network requests that are made by a page. Once request interception is enabled, every request will stall unless it's continued, responded or aborted; or completed using the browser cache. See the Request interception guide for more details. | |
setUserAgent(userAgent, userAgentMetadata) | ||
setViewport(viewport) |
In the case of multiple pages in a single browser, each page can have its own viewport size. | |
tap(selector) | This method fetches an element with | |
target() |
| A target this page was created from. Deprecated: Use Page.createCDPSession() directly. |
title() | The page's title | |
type(selector, text, options) | Sends a To press a special key, like | |
url() | The page's URL. | |
viewport() | Returns the current page viewport settings without checking the actual page viewport. This is either the viewport set with the previous Page.setViewport() call or the default viewport set via BrowserConnectOptions.defaultViewport. | |
waitForDevicePrompt(options) | This method is typically coupled with an action that triggers a device request from an api such as WebBluetooth. caution This must be called before the device request is made. It will not return a currently active device prompt. | |
waitForFileChooser(options) | This method is typically coupled with an action that triggers file choosing. caution This must be called before the file chooser is launched. It will not return a currently active file chooser. | |
waitForFrame(urlOrPredicate, options) | Waits for a frame matching the given conditions to appear. | |
waitForFunction(pageFunction, options, args) | Waits for the provided function, | |
Waits for the page to navigate to a new URL or to reload. It is useful when you run code that will indirectly cause the page to navigate. | ||
waitForNetworkIdle(options) | Waits for the network to be idle. | |
waitForRequest(urlOrPredicate, options) | ||
waitForResponse(urlOrPredicate, options) | ||
waitForSelector(selector, options) | Wait for the | |
workers() | All of the dedicated WebWorkers associated with the page. |