Skip to main content

CDPSession

CDPSession 实例用于直接使用原生的 Chrome Devtools 协议进行通信:

🌐 The CDPSession instances are used to talk raw Chrome Devtools Protocol:

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

有用的链接:

🌐 Useful links:

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.DetachAsync

将 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.

用法

await CdpSession.DetachAsync();

返回


Event

Added in: v.1.30 cdpSession.Event

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

🌐 Returns an event emitter for the given CDP event name.

用法

CdpSession.Event(eventName);

参数

  • eventName string Added in: v1.30#

    CDP 事件名称。

返回


SendAsync

Added before v1.9 cdpSession.SendAsync

用法

await CdpSession.SendAsync(method, params);

参数

  • method string#

    协议方法名称。

  • args [Map]?<string, Args> (optional) Added in: v1.30#

    可选方法参数。

返回

  • [JsonElement?]#