CDPSession
CDPSession 实例用于直接使用原生的 Chrome Devtools 协议进行通信:
🌐 The CDPSession instances are used to talk raw Chrome Devtools Protocol:
- 协议方法可以通过
session.send方法调用。 - 可以使用
session.on方法订阅协议事件。
有用的链接:
🌐 Useful links:
- 关于 DevTools 协议的文档可以在这里找到:DevTools 协议查看器。
- DevTools 协议入门: https://github.com/aslushnikov/getting-started-with-cdp/blob/master/README.md
- Sync
- Async
client = page.context.new_cdp_session(page)
client.send("Animation.enable")
client.on("Animation.animationCreated", lambda: print("animation created!"))
response = client.send("Animation.getPlaybackRate")
print("playback rate is " + str(response["playbackRate"]))
client.send("Animation.setPlaybackRate", {
"playbackRate": response["playbackRate"] / 2
})
client = await page.context.new_cdp_session(page)
await client.send("Animation.enable")
client.on("Animation.animationCreated", lambda: print("animation created!"))
response = await client.send("Animation.getPlaybackRate")
print("playback rate is " + str(response["playbackRate"]))
await client.send("Animation.setPlaybackRate", {
"playbackRate": response["playbackRate"] / 2
})
方法
🌐 Methods
detach
Added before v1.9将 CDPSession 从目标中分离。一旦分离,CDPSession 对象将不会发出任何事件,也不能用于发送消息。
🌐 Detaches the CDPSession from the target. Once detached, the CDPSession object won't emit any events and can't be used to send messages.
用法
cdp_session.detach()
返回
send
Added before v1.9用法
cdp_session.send(method)
cdp_session.send(method, **kwargs)
参数
返回