Puppeteer 中文文档v25.8.0

CDPSession 类

CDPSession 实例用于直接与 Chrome DevTools 协议进行通信。

签名

export declare abstract class CDPSession extends EventEmitter<CDPSessionEvents>

继承自: EventEmitter<CDPSessionEvents>

备注

可以使用 CDPSession.send() 方法调用协议方法,也可以使用 CDPSession.on 方法订阅协议事件。

相关链接:DevTools Protocol ViewerGetting Started with DevTools Protocol

此类的构造函数被标记为内部(internal)。第三方代码不应直接调用构造函数,也不应创建继承 CDPSession 类的子类。

示例

const client = await page.createCDPSession();
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,
});

属性

属性

修饰符

类型

说明

detached

readonly

boolean

如果会话已被分离,则为 true,否则为 false。

方法

方法

修饰符

说明

connection()

该会话的底层连接(如果有的话)。

detach()

将 cdpSession 从目标中分离。分离后,cdpSession 对象将不再发出任何事件,也不能用于发送消息。

id()

返回会话的 id。

send(method, params, options)