CDPSession
CDPSession
实例用于讨论原始 Chrome Devtools 协议:
¥The CDPSession
instances are used to talk raw Chrome Devtools Protocol:
-
协议方法可以通过
session.send
方法调用。¥protocol methods can be called with
session.send
method. -
可以使用
session.on
方法订阅协议事件。¥protocol events can be subscribed to with
session.on
method.
有用的链接:
¥Useful links:
-
有关 DevTools 协议的文档可以在这里找到:DevTools 协议查看器。
¥Documentation on DevTools Protocol can be found here: DevTools Protocol Viewer.
-
DevTools 协议入门:https://github.com/aslushnikov/getting-started-with-cdp/blob/master/README.md
¥Getting Started with DevTools Protocol: https://github.com/aslushnikov/getting-started-with-cdp/blob/master/README.md
var client = await Page.Context.NewCDPSessionAsync(Page);
await client.SendAsync("Runtime.enable");
client.Event("Animation.animationCreated").OnEvent += (_, _) => Console.WriteLine("Animation created!");
var response = await client.SendAsync("Animation.getPlaybackRate");
var playbackRate = response.Value.GetProperty("playbackRate").GetDouble();
Console.WriteLine("playback rate is " + playbackRate);
await client.SendAsync("Animation.setPlaybackRate", new() { { "playbackRate", playbackRate / 2 } });
方法
¥Methods
DetachAsync
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.
用法
¥Usage
await CdpSession.DetachAsync();
返回
¥Returns
事件
¥Event
Added in: v.1.30返回给定 CDP 事件名称的事件触发器。
¥Returns an event emitter for the given CDP event name.
用法
¥Usage
CdpSession.Event(eventName);
参数
¥Arguments
CDP 事件名称。
¥CDP event name.
返回
¥Returns
SendAsync
Added before v1.9用法
¥Usage
await CdpSession.SendAsync(method, params);
参数
¥Arguments
协议方法名称。
¥Protocol method name.
可选方法参数。
¥Optional method parameters.
返回
¥Returns
- [JsonElement?]#