跳到主要内容

CDPSession

CDPSession 实例用于进行原始 Chrome Devtools Protocol 通信

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

有用链接

CDPSession client = page.context().newCDPSession(page);
client.send("Runtime.enable");

client.on("Animation.animationCreated", (event) -> System.out.println("Animation created!"));

JsonObject response = client.send("Animation.getPlaybackRate");
double playbackRate = response.get("playbackRate").getAsDouble();
System.out.println("playback rate is " + playbackRate);

JsonObject params = new JsonObject();
params.addProperty("playbackRate", playbackRate / 2);
client.send("Animation.setPlaybackRate", params);

方法

detach

在 v1.9 之前添加 cdpSession.detach

将 CDPSession 从目标分离。分离后,CDPSession 对象将不会发出任何事件,也不能用于发送消息。

用法

CDPSession.detach();

返回


off

添加于: v1.37 cdpSession.off

取消注册指定事件名称的事件处理程序。给定的处理程序将不再被调用来处理具有给定名称的事件。

用法

CDPSession.off(eventName, handler);

参数


on

添加于: v1.37 cdpSession.on

注册指定事件名称的事件处理程序。给定的处理程序将为具有给定名称的每个事件调用。

用法

CDPSession.on(eventName, handler);

参数


send

在 v1.9 之前添加 cdpSession.send

用法

CDPSession.send(method);
CDPSession.send(method, args);

参数

  • method String#

    协议方法名称。

  • args JsonObject (可选)添加于: v1.37#

    可选的方法参数。

返回