Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/pptr/src/Page.ts>Page

Headless Chrome Deno API
Latest
class Page
extends EventEmitter
import { Page } from "https://deno.land/x/pptr@1.2.0/src/Page.ts";

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

Examples

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.

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);

Constructors

new
Page(
client: CDPSession,
target: Target,
ignoreHTTPSErrors: boolean,
)

Properties

private
_accessibility: Accessibility
private
_client: CDPSession
private
_closed: boolean
private
_coverage: Coverage
private
optional
_disconnectPromise: Promise<Error>
private
_emulationManager: EmulationManager
private
_fileChooserInterceptors: Set<Function>
private
_frameManager: FrameManager
private
_javascriptEnabled: boolean
private
_keyboard: Keyboard
private
_mouse: Mouse
private
_pageBindings: Map<string, Function>
private
_screenshotTaskQueue: ScreenshotTaskQueue
private
_target: Target
private
_timeoutSettings: TimeoutSettings
private
_touchscreen: Touchscreen
private
_tracing: Tracing
private
_viewport: Viewport | null
private
_workers: Map<string, WebWorker>
readonly
accessibility: Accessibility
readonly
coverage: Coverage
readonly
keyboard: Keyboard
readonly
mouse: Mouse
readonly
touchscreen: Touchscreen
readonly
tracing: Tracing

Methods

private
_addConsoleMessage(
type: ConsoleMessageType | string,
args: JSHandle[],
stackTrace?: Protocol.Runtime.StackTrace,
): void
private
_buildMetricsObject(metrics?: Protocol.Performance.Metric[]): Metrics
private
_emitMetrics(event: Protocol.Performance.MetricsEvent): void
private
_go(delta: number, options: WaitForOptions): Promise<HTTPResponse | null>
private
_handleException(exceptionDetails: Protocol.Runtime.ExceptionDetails): void
private
_initialize(): Promise<void>
private
_onBindingCalled(event: Protocol.Runtime.BindingCalledEvent): Promise<void>
private
_onConsoleAPI(event: Protocol.Runtime.ConsoleAPICalledEvent): Promise<void>
private
_onDialog(event: Protocol.Page.JavascriptDialogOpeningEvent): void
private
_onFileChooser(event: Protocol.Page.FileChooserOpenedEvent): Promise<void>
private
_onLogEntryAdded(event: Protocol.Log.EntryAddedEvent): void
private
_onTargetCrashed(): void
private
_screenshotTask(format: "png" | "jpeg", options: ScreenshotOptions): Promise<Uint8Array | string>
private
_sessionClosePromise(): Promise<Error>
$(selector: string): Promise<ElementHandle | null>

Runs document.querySelector within the page. If no element matches the selector, the return value resolves to null.

$$(selector: string): Promise<ElementHandle[]>
$$eval<ReturnType>(
selector: string,
pageFunction: (elements: Element[], ...args: unknown[]) => ReturnType | Promise<ReturnType>,
): Promise<WrapElementHandle<ReturnType>>

This method runs Array.from(document.querySelectorAll(selector)) within the page and passes the result as the first argument to the pageFunction.

$eval<ReturnType>(
selector: string,
pageFunction: (element: Element, ...args: unknown[]) => ReturnType | Promise<ReturnType>,
): Promise<WrapElementHandle<ReturnType>>

This method runs document.querySelector within the page and passes the result as the first argument to the pageFunction.

$x(expression: string): Promise<ElementHandle[]>
addScriptTag(options: { url?: string; path?: string; content?: string; type?: string; }): Promise<ElementHandle>
addStyleTag(options: { url?: string; path?: string; content?: string; }): Promise<ElementHandle>
authenticate(credentials: Credentials): Promise<void>
bringToFront(): Promise<void>
click(selector: string, options?: { delay?: number; button?: MouseButton; clickCount?: number; }): Promise<void>
close(options?: { runBeforeUnload?: boolean; }): Promise<void>
content(): Promise<string>
cookies(...urls: string[]): Promise<Protocol.Network.Cookie[]>

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: Protocol.Network.DeleteCookiesRequest[]): Promise<void>
emulate(options: { viewport: Viewport; userAgent: string; }): Promise<void>
emulateIdleState(overrides?: { isUserActive: boolean; isScreenUnlocked: boolean; }): Promise<void>

Emulates the idle state. If no arguments set, clears idle state emulation.

emulateMediaFeatures(features?: MediaFeature[]): Promise<void>
emulateMediaType(type?: string): Promise<void>
emulateTimezone(timezoneId?: string): Promise<void>
emulateVisionDeficiency(type?: Protocol.Emulation.SetEmulatedVisionDeficiencyRequest["type"]): Promise<void>

Simulates the given vision deficiency on the page.

evaluate<T extends EvaluateFn>(pageFunction: T, ...args: SerializableOrJSHandle[]): Promise<UnwrapPromiseLike<EvaluateFnReturnType<T>>>
evaluateHandle<HandlerType extends JSHandle = JSHandle>(pageFunction: EvaluateHandleFn, ...args: SerializableOrJSHandle[]): Promise<HandlerType>
evaluateOnNewDocument(pageFunction: Function | string, ...args: unknown[]): Promise<void>
exposeFunction(name: string, puppeteerFunction: Function): Promise<void>
focus(selector: string): Promise<void>
goBack(options?: WaitForOptions): Promise<HTTPResponse | null>
goForward(options?: WaitForOptions): Promise<HTTPResponse | null>
goto(url: string, options?: WaitForOptions & { referer?: string; }): Promise<HTTPResponse | null>
hover(selector: string): Promise<void>
isClosed(): boolean
metrics(): Promise<Metrics>
pdf(options?: PDFOptions): Promise<Uint8Array>

Generatees a PDF of the page with the print CSS media type.

queryObjects(prototypeHandle: JSHandle): Promise<JSHandle>

This method iterates the JavaScript heap and finds all objects with the given prototype.

reload(options?: WaitForOptions): Promise<HTTPResponse | null>
screenshot(options?: ScreenshotOptions): Promise<Uint8Array | string | void>
select(selector: string, ...values: string[]): Promise<string[]>
setBypassCSP(enabled: boolean): Promise<void>
setCacheEnabled(enabled?): Promise<void>
setContent(html: string, options?: WaitForOptions): Promise<void>
setCookie(...cookies: Protocol.Network.CookieParam[]): Promise<void>
setDefaultNavigationTimeout(timeout: number): void
setDefaultTimeout(timeout: number): void
setExtraHTTPHeaders(headers: Record<string, string>): Promise<void>
setGeolocation(options: GeolocationOptions): Promise<void>

Sets the page's geolocation.

setJavaScriptEnabled(enabled: boolean): Promise<void>
setOfflineMode(enabled: boolean): Promise<void>
setRequestInterception(value: boolean): Promise<void>
setUserAgent(userAgent: string): Promise<void>
setViewport(viewport: Viewport): Promise<void>
tap(selector: string): Promise<void>
title(): Promise<string>
type(
selector: string,
text: string,
options?: { delay: number; },
): Promise<void>
url(): string
viewport(): Viewport | null
waitForFunction(
pageFunction: Function | string,
options?: { timeout?: number; polling?: string | number; },
): Promise<JSHandle>
waitForNavigation(options?: WaitForOptions): Promise<HTTPResponse | null>
waitForRequest(urlOrPredicate: string | Function, options?: { timeout?: number; }): Promise<HTTPRequest>
waitForResponse(urlOrPredicate: string | Function, options?: { timeout?: number; }): Promise<HTTPResponse>
waitForSelector(selector: string, options?: { visible?: boolean; hidden?: boolean; timeout?: number; }): Promise<ElementHandle | null>
waitForTimeout(milliseconds: number): Promise<void>

Causes your script to wait for the given number of milliseconds.

waitForXPath(xpath: string, options?: { visible?: boolean; hidden?: boolean; timeout?: number; }): Promise<ElementHandle | null>

Static Methods

create(
client: CDPSession,
target: Target,
ignoreHTTPSErrors: boolean,
defaultViewport: Viewport | null,
): Promise<Page>