CDPSession
CDPSession
实例用于进行原始 Chrome Devtools Protocol 通信
- 协议方法可以通过
session.send
方法调用。 - 协议事件可以通过
session.on
方法订阅。
有用链接
- DevTools Protocol 文档可以在这里找到: DevTools Protocol Viewer。
- DevTools Protocol 入门: https://github.com/aslushnikov/getting-started-with-cdp/blob/master/README.md
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 从目标分离。分离后,CDPSession 对象将不会发出任何事件,也不能用于发送消息。
用法
CDPSession.detach();
返回
off
添加于: v1.37取消注册指定事件名称的事件处理程序。给定的处理程序将不再被调用来处理具有给定名称的事件。
用法
CDPSession.off(eventName, handler);
参数
-
CDP 事件名称。
-
handler
Consumer<JsonObject>#事件处理程序。
on
添加于: v1.37注册指定事件名称的事件处理程序。给定的处理程序将为具有给定名称的每个事件调用。
用法
CDPSession.on(eventName, handler);
参数
-
CDP 事件名称。
-
handler
Consumer<JsonObject>#事件处理程序。
send
在 v1.9 之前添加用法
CDPSession.send(method);
CDPSession.send(method, args);
参数
-
协议方法名称。
-
args
JsonObject (可选)添加于: v1.37#可选的方法参数。
返回