跳至主要内容

CDPSession

CDPSession 实例用于直接与 Chrome Devtools 协议通信:

  • 协议方法可以通过session.send方法调用。
  • 可以通过session.on方法订阅协议事件。

有用的链接:

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 } });

方法

DetachAsync

Added before v1.9 cdpSession.DetachAsync

将CDPSession从目标分离。一旦分离,CDPSession对象将不再发出任何事件,也无法用于发送消息。

用法

await CdpSession.DetachAsync();

返回


事件

Added in: v.1.30 cdpSession.Event

返回给定CDP事件名称的事件发射器。

用法

CdpSession.Event(eventName);

参数

  • eventName string 新增于: v1.30#

    CDP事件名称。

返回


SendAsync

Added before v1.9 cdpSession.SendAsync

用法

await CdpSession.SendAsync(method, params);

参数

  • method string#

    协议方法名称。

  • args [Map]?<string, Args> (可选) 添加于: v1.30#

    可选的方法参数。

返回

  • [JsonElement?]#