CDPSession
CDPSession 实例用于直接使用原生的 Chrome Devtools 协议进行通信:
🌐 The CDPSession instances are used to talk raw Chrome Devtools Protocol:
- 协议方法可以通过
session.send方法调用。 - 可以使用
session.on方法订阅协议事件。
有用的链接:
🌐 Useful links:
- 关于 DevTools 协议的文档可以在这里找到:DevTools 协议查看器。
- 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);
方法
🌐 Methods
detach
Added before v1.9将 CDPSession 从目标中分离。一旦分离,CDPSession 对象将不会发出任何事件,也不能用于发送消息。
🌐 Detaches the CDPSession from the target. Once detached, the CDPSession object won't emit any events and can't be used to send messages.
用法
CDPSession.detach();
返回
off
Added in: v1.37注销指定事件名称的事件处理程序。指定的处理程序将不再被调用用于该名称的事件。
🌐 Unregister an event handler for events with the specified event name. The given handler will not be called anymore for events with the given name.
用法
CDPSession.off(eventName, handler);
参数
-
CDP 事件名称。
-
handlerConsumer<JsonObject>#事件处理程序。
on
Added in: v1.37为具有指定事件名称的事件注册事件处理程序。每当有该名称的事件发生时,将调用给定的处理程序。
🌐 Register an event handler for events with the specified event name. The given handler will be called for every event with the given name.
用法
CDPSession.on(eventName, handler);
参数
-
CDP 事件名称。
-
handlerConsumer<JsonObject>#事件处理程序。
send
Added before v1.9用法
CDPSession.send(method);
CDPSession.send(method, args);
参数
-
协议方法名称。
-
argsJsonObject (optional) Added in: v1.37#可选方法参数。
返回