CDPSession
CDPSession
实例用于直接与 Chrome DevTools 协议进行通信:
- 可以通过
session.send
方法调用协议方法 - 可以通过
session.on
方法订阅协议事件
有用的链接:
- DevTools 协议文档可以在这里找到:DevTools 协议查看器
- DevTools 协议入门指南:https://github.com/aslushnikov/getting-started-with-cdp/blob/master/README.md
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
});
方法
detach
在 v1.9 之前添加将 CDPSession 从目标对象分离。一旦分离,CDPSession 对象将不再发出任何事件,也不能用于发送消息。
用法
await cdpSession.detach();
返回值
send
v1.9 版本前添加用法
await cdpSession.send(method);
await cdpSession.send(method, params);
参数
返回值