CDPSession
CDPSession
实例用于直接与 Chrome Devtools 协议通信:
- 协议方法可以通过
session.send
方法调用。 - 可以通过
session.on
方法订阅协议事件。
有用的链接:
- 关于DevTools协议的文档可以在这里找到:DevTools Protocol Viewer。
- DevTools协议入门指南: 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
Added before v1.9将CDPSession从目标分离。一旦分离,CDPSession对象将不再发出任何事件,也无法用于发送消息。
用法
CDPSession.detach();
返回
关闭
Added in: v1.37取消注册具有指定事件名称的事件处理程序。对于给定名称的事件,将不再调用给定的处理程序。
用法
CDPSession.off(eventName, handler);
参数
-
CDP事件名称。
-
handler
Consumer<JsonObject>#事件处理器。
开启
Added in: v1.37为指定名称的事件注册一个事件处理程序。每当发生具有该名称的事件时,给定的处理程序都会被调用。
用法
CDPSession.on(eventName, handler);
参数
-
CDP事件名称。
-
handler
Consumer<JsonObject>#事件处理程序。
发送
Added before v1.9用法
CDPSession.send(method);
CDPSession.send(method, args);
参数
-
协议方法名称。
-
args
JsonObject (可选) 添加于: v1.37#可选的方法参数。
返回