跳转到主要内容

CDPSession

CDPSession 实例用于与原始 Chrome Devtools 协议通信

  • 协议方法可以通过 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#

    可选方法参数。

返回