puppeteer/new-docs/puppeteer.page.md
Maksim Sadym 17960e5d8d
feat(page): emulate idle state (#6410)
* https://bugs.chromium.org/p/chromium/issues/detail?id=1090802
* added `page.emulateIdleState(...)` allowing emulate or remove emulation of the idle state;
* added test `emulate idle` -> `remove emulation` -> `emulate idle` -> `remove emulation`;
* added launch argument `--enable-blink-features=IdleDetection` to turn IdleDetection on.
2020-09-14 11:31:23 +02:00

8.2 KiB

Home > puppeteer > Page

Page class

Page provides methods to interact with a single tab or extension background page in Chromium.

Signature:

export declare class Page extends EventEmitter 

Extends: EventEmitter

Remarks

One Browser instance might have multiple Page instances.

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:

const puppeteer = require('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 PageEmittedEvents 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 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 Accessibility
coverage Coverage
keyboard Keyboard
mouse Mouse
touchscreen Touchscreen
tracing Tracing

Methods

Method Modifiers Description
$(selector) Runs document.querySelector within the page. If no element matches the selector, the return value resolves to null.
$$(selector)
$$eval(selector, pageFunction, args) This method runs Array.from(document.querySelectorAll(selector)) within the page and passes the result as the first argument to the pageFunction.
$eval(selector, pageFunction, args) This method runs document.querySelector within the page and passes the result as the first argument to the pageFunction.
$x(expression)
addScriptTag(options)
addStyleTag(options)
authenticate(credentials)
bringToFront()
browser()
browserContext()
click(selector, options)
close(options)
content()
cookies(urls) 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.
deleteCookie(cookies)
emulate(options)
emulateIdleState(overrides) Emulates the idle state. If no arguments set, clears idle state emulation.
emulateMediaFeatures(features)
emulateMediaType(type)
emulateTimezone(timezoneId)
emulateVisionDeficiency(type) Simulates the given vision deficiency on the page.
evaluate(pageFunction, args)
evaluateHandle(pageFunction, args)
evaluateOnNewDocument(pageFunction, args)
exposeFunction(name, puppeteerFunction)
focus(selector)
frames()
goBack(options)
goForward(options)
goto(url, options)
hover(selector)
isClosed()
isJavaScriptEnabled()
mainFrame()
metrics()
pdf(options) Generatees a PDF of the page with the print CSS media type.
queryObjects(prototypeHandle) This method iterates the JavaScript heap and finds all objects with the given prototype.
reload(options)
screenshot(options)
select(selector, values)
setBypassCSP(enabled)
setCacheEnabled(enabled)
setContent(html, options)
setCookie(cookies)
setDefaultNavigationTimeout(timeout)
setDefaultTimeout(timeout)
setExtraHTTPHeaders(headers)
setGeolocation(options) Sets the page's geolocation.
setJavaScriptEnabled(enabled)
setOfflineMode(enabled)
setRequestInterception(value)
setUserAgent(userAgent)
setViewport(viewport)
tap(selector)
target()
title()
type(selector, text, options)
url()
viewport()
waitFor(selectorOrFunctionOrTimeout, options, args)
waitForFileChooser(options)
waitForFunction(pageFunction, options, args)
waitForNavigation(options)
waitForRequest(urlOrPredicate, options)
waitForResponse(urlOrPredicate, options)
waitForSelector(selector, options)
waitForTimeout(milliseconds) Causes your script to wait for the given number of milliseconds.
waitForXPath(xpath, options)
workers()