跳至主要内容

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

Added before v1.9 cdpSession.detach

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

用法

CDPSession.detach();

返回


关闭

Added in: v1.37 cdpSession.off

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

用法

CDPSession.off(eventName, handler);

参数


开启

Added in: v1.37 cdpSession.on

为指定名称的事件注册一个事件处理程序。每当发生具有该名称的事件时,给定的处理程序都会被调用。

用法

CDPSession.on(eventName, handler);

参数


发送

Added before v1.9 cdpSession.send

用法

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

参数

  • method String#

    协议方法名称。

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

    可选的方法参数。

返回