HyperionGray / trio-chrome-devtools-protocol

Trio driver for Chrome DevTools Protocol (CDP)
MIT License
60 stars 17 forks source link

Event example ? #14

Open benoitgaly opened 4 years ago

benoitgaly commented 4 years ago

Hi,

If it is possible in the documentation to understand how to use trio_cdp for event management like cdp.network.RequestWillBeSent ... it would be of great help

mehaase commented 4 years ago

The documentation definitely needs to be improved for event handling. On the Trio CDP side, there are two APIs. The first API waits for a single event and returns the result.

async with session.wait_for(network.RequestWillBeSent) as result:
   await ... # do something here that triggers an event
print(result.value) # the value is available after leaving the block

The second API is for getting a stream of events:

async for event in session.listen(network.RequestWillBeSent):
  print("got event", event)

Since this one loops over the events, you'll want to call it in a new task so that you can keep doing work on the main task.

On the Chrome side, the protocol events are documented pretty poorly. I've figured out a few of them by trial and error, though. With the network domain, you will need to call network.enable() prior to getting any events. You might also try the fetch domain which has similar features but is a little friendlier.