2021-01-07 20:46:05 +01:00
|
|
|
# class: CDPSession
|
2022-07-06 02:24:50 +02:00
|
|
|
* since: v1.8
|
2021-01-07 20:46:05 +01:00
|
|
|
|
|
|
|
|
The `CDPSession` instances are used to talk raw Chrome Devtools Protocol:
|
|
|
|
|
* protocol methods can be called with `session.send` method.
|
|
|
|
|
* protocol events can be subscribed to with `session.on` method.
|
|
|
|
|
|
|
|
|
|
Useful links:
|
2021-01-14 16:48:56 +01:00
|
|
|
* Documentation on DevTools Protocol can be found here:
|
|
|
|
|
[DevTools Protocol Viewer](https://chromedevtools.github.io/devtools-protocol/).
|
|
|
|
|
* Getting Started with DevTools Protocol:
|
|
|
|
|
https://github.com/aslushnikov/getting-started-with-cdp/blob/master/README.md
|
2021-01-07 20:46:05 +01:00
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const client = await page.context().newCDPSession(page);
|
|
|
|
|
await client.send('Animation.enable');
|
|
|
|
|
client.on('Animation.animationCreated', () => console.log('Animation created!'));
|
|
|
|
|
const response = await client.send('Animation.getPlaybackRate');
|
|
|
|
|
console.log('playback rate is ' + response.playbackRate);
|
|
|
|
|
await client.send('Animation.setPlaybackRate', {
|
|
|
|
|
playbackRate: response.playbackRate / 2
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
2021-01-14 16:48:56 +01:00
|
|
|
```python async
|
2021-09-14 11:18:28 +02:00
|
|
|
client = await page.context.new_cdp_session(page)
|
2021-06-25 20:53:55 +02:00
|
|
|
await client.send("Animation.enable")
|
|
|
|
|
client.on("Animation.animationCreated", lambda: print("animation created!"))
|
|
|
|
|
response = await client.send("Animation.getPlaybackRate")
|
2021-06-26 17:30:06 +02:00
|
|
|
print("playback rate is " + str(response["playbackRate"]))
|
|
|
|
|
await client.send("Animation.setPlaybackRate", {
|
2024-04-29 12:58:59 +02:00
|
|
|
"playbackRate": response["playbackRate"] / 2
|
2021-01-14 16:48:56 +01:00
|
|
|
})
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```python sync
|
2021-09-14 11:18:28 +02:00
|
|
|
client = page.context.new_cdp_session(page)
|
2021-06-25 20:53:55 +02:00
|
|
|
client.send("Animation.enable")
|
|
|
|
|
client.on("Animation.animationCreated", lambda: print("animation created!"))
|
|
|
|
|
response = client.send("Animation.getPlaybackRate")
|
2021-06-26 17:30:06 +02:00
|
|
|
print("playback rate is " + str(response["playbackRate"]))
|
2021-06-25 20:53:55 +02:00
|
|
|
client.send("Animation.setPlaybackRate", {
|
2024-04-29 12:58:59 +02:00
|
|
|
"playbackRate": response["playbackRate"] / 2
|
2021-01-14 16:48:56 +01:00
|
|
|
})
|
|
|
|
|
```
|
2023-01-26 11:49:10 +01:00
|
|
|
```csharp
|
|
|
|
|
var client = await Page.Context.NewCDPSessionAsync(Page);
|
|
|
|
|
await client.SendAsync("Runtime.enable");
|
|
|
|
|
client.Event("Animation.animationCreated").OnEvent += (_, _) => Console.WriteLine("Animation created!");
|
|
|
|
|
var response = await client.SendAsync("Animation.getPlaybackRate");
|
|
|
|
|
var playbackRate = response.Value.GetProperty("playbackRate").GetDouble();
|
|
|
|
|
Console.WriteLine("playback rate is " + playbackRate);
|
|
|
|
|
await client.SendAsync("Animation.setPlaybackRate", new() { { "playbackRate", playbackRate / 2 } });
|
|
|
|
|
```
|
2023-07-17 19:12:04 +02:00
|
|
|
```java
|
|
|
|
|
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);
|
|
|
|
|
```
|
2021-01-14 16:48:56 +01:00
|
|
|
|
2021-01-07 20:46:05 +01:00
|
|
|
## async method: CDPSession.detach
|
2022-07-06 02:24:50 +02:00
|
|
|
* since: v1.8
|
2021-01-07 20:46:05 +01:00
|
|
|
|
|
|
|
|
Detaches the CDPSession from the target. Once detached, the CDPSession object won't emit any events and can't be used to
|
|
|
|
|
send messages.
|
|
|
|
|
|
|
|
|
|
## async method: CDPSession.send
|
2022-07-06 02:24:50 +02:00
|
|
|
* since: v1.8
|
2021-01-07 20:46:05 +01:00
|
|
|
- returns: <[Object]>
|
|
|
|
|
|
2023-01-26 11:49:10 +01:00
|
|
|
## async method: CDPSession.send
|
|
|
|
|
* since: v1.30
|
|
|
|
|
* langs: csharp
|
|
|
|
|
- returns: <[JsonElement?]>
|
|
|
|
|
|
2023-07-17 19:12:04 +02:00
|
|
|
## async method: CDPSession.send
|
|
|
|
|
* since: v1.37
|
|
|
|
|
* langs: java
|
|
|
|
|
- returns: <[JsonObject]>
|
|
|
|
|
|
2021-01-07 20:46:05 +01:00
|
|
|
### param: CDPSession.send.method
|
2022-07-06 02:24:50 +02:00
|
|
|
* since: v1.8
|
2021-01-07 20:46:05 +01:00
|
|
|
- `method` <[string]>
|
|
|
|
|
|
2022-04-07 04:02:10 +02:00
|
|
|
Protocol method name.
|
2021-01-07 20:46:05 +01:00
|
|
|
|
|
|
|
|
### param: CDPSession.send.params
|
2022-07-06 02:24:50 +02:00
|
|
|
* since: v1.8
|
2023-01-26 11:49:10 +01:00
|
|
|
* langs: js, python
|
2022-04-07 04:02:10 +02:00
|
|
|
- `params` ?<[Object]>
|
2021-01-07 20:46:05 +01:00
|
|
|
|
2022-04-07 04:02:10 +02:00
|
|
|
Optional method parameters.
|
2023-01-26 11:49:10 +01:00
|
|
|
|
|
|
|
|
### param: CDPSession.send.params
|
|
|
|
|
* since: v1.30
|
|
|
|
|
* langs: csharp
|
|
|
|
|
- alias-csharp: args
|
|
|
|
|
- `params` ?<[Map<string, Object>]>
|
|
|
|
|
|
|
|
|
|
Optional method parameters.
|
|
|
|
|
|
2023-07-17 19:12:04 +02:00
|
|
|
### param: CDPSession.send.params
|
|
|
|
|
* since: v1.37
|
|
|
|
|
* langs: java
|
|
|
|
|
- alias-java: args
|
|
|
|
|
- `params` ?<[JsonObject]>
|
|
|
|
|
|
|
|
|
|
Optional method parameters.
|
|
|
|
|
|
2023-01-26 11:49:10 +01:00
|
|
|
## method: CDPSession.event
|
|
|
|
|
* since: v.1.30
|
|
|
|
|
* langs: csharp
|
|
|
|
|
- returns: <[CDPSessionEvent]>
|
|
|
|
|
|
|
|
|
|
Returns an event emitter for the given CDP event name.
|
|
|
|
|
|
|
|
|
|
### param: CDPSession.event.eventName
|
|
|
|
|
* since: v1.30
|
|
|
|
|
* langs: csharp
|
|
|
|
|
- `eventName` <[string]>
|
|
|
|
|
|
2023-07-17 19:12:04 +02:00
|
|
|
CDP event name.
|
|
|
|
|
|
|
|
|
|
## method: CDPSession.on
|
|
|
|
|
* since: v1.37
|
|
|
|
|
* langs: java
|
|
|
|
|
|
|
|
|
|
Register an event handler for events with the specified event name.
|
|
|
|
|
The given handler will be called for every event with the given name.
|
|
|
|
|
|
|
|
|
|
### param: CDPSession.on.eventName
|
|
|
|
|
* since: v1.37
|
|
|
|
|
- `eventName` <[string]>
|
|
|
|
|
|
|
|
|
|
CDP event name.
|
|
|
|
|
|
|
|
|
|
### param: CDPSession.on.handler
|
|
|
|
|
* since: v1.37
|
|
|
|
|
- `handler` <[function]\([JsonObject]\)>
|
|
|
|
|
|
|
|
|
|
Event handler.
|
|
|
|
|
|
|
|
|
|
## method: CDPSession.off
|
|
|
|
|
* since: v1.37
|
|
|
|
|
* langs: java
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
### param: CDPSession.off.eventName
|
|
|
|
|
* since: v1.37
|
|
|
|
|
- `eventName` <[string]>
|
|
|
|
|
|
|
|
|
|
CDP event name.
|
|
|
|
|
|
|
|
|
|
### param: CDPSession.off.handler
|
|
|
|
|
* since: v1.37
|
|
|
|
|
- `handler` <[function]\([JsonObject]\)>
|
|
|
|
|
|
|
|
|
|
Event handler.
|