跳到正文
本页尚未翻译成中文,以下为英文原文。 查看英文版

CDPSession ​

Added in: v1.8

The CDPSession instances are used to talk raw Chrome Devtools Protocol:

  • protocol methods can be called with session.send method.
  • protocol events can be subscribed to with session.on method.

Useful links:

js
const client = await page.context().newCDPSession(page);
await client.send('Animation.enable');
client.on('Animation.animationCreated', () => console.log('Animation created!'));
const response = await client.send('Animation.getPlaybackRate');
console.log('playback rate is ' + response.playbackRate);
await client.send('Animation.setPlaybackRate', {
  playbackRate: response.playbackRate / 2
});

Methods ​

cdpSession.detach() ​

Added in: v1.8 async

Detaches the CDPSession from the target. Once detached, the CDPSession object won't emit any events and can't be used to send messages.

cdpSession.send() ​

Added in: v1.8 async

Arguments

  • method string

    Protocol method name.

  • params Object (optional)

    Optional method parameters.

  • options Object (optional)

    • signal AbortSignal (optional) Added in: v1.65

      Allows to cancel the operation using an AbortSignal. If the signal is aborted, the operation will be aborted and throw an error.

    • timeout float (optional) Added in: v1.65

      Maximum time in milliseconds to wait for the response. Defaults to 0 - no timeout.

Returns

  • Promise<Object>

Events ​

cdpSession.on('close') ​

Added in: v1.59

Emitted when the session is closed, either because the target was closed or session.detach() was called.

Event data

  • CDPSession

cdpSession.on('event') ​

Added in: v1.59

Emitted for every CDP event received from the session. Allows subscribing to all CDP events at once without knowing their names ahead of time.

Usage

js
session.on('event', ({ method, params }) => {
  console.log(`CDP event: ${method}`, params);
});

Event data

  • Object

基于 Apache 2.0 许可证发布。