Reputation: 4727
I've built and installed Selenium 4.0.0-beta-1 python wheel from source to test CDP functionalities. Specifically I would like to intercept requests using Fetch Domain protocol.
I can enable the domain using Fetch.enable
command, but I don't see how I can subscribe to events like Fetch.requestPaused to intercept the request:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
driver = webdriver.Chrome()
# Enable Fetch domain
driver.execute_cdp_cmd('Fetch.enable', cmd_args={})
# How to subscribe to Fetch.requestPaused event??
# driver.add_cdp_event_listener ...
Thanks for any help!
Upvotes: 5
Views: 3845
Reputation: 381
Tested on selenium 4.0.0rc1, with chromedriver v94 (latest as of 9/21/2021).
import trio # async library that selenium uses
from selenium import webdriver
async def start_listening(listener):
async for event in listener:
print(event)
async def main():
driver = webdriver.Chrome()
async with driver.bidi_connection() as connection:
session, devtools = connection.session, connection.devtools
# await session.execute(devtools.fetch.enable())
await session.execute(devtools.network.enable())
# listener = session.listen(devtools.fetch.RequestPaused)
listener = session.listen(devtools.network.ResponseReceived)
async with trio.open_nursery() as nursery:
nursery.start_soon(start_listening, listener) # start_listening blocks, so we run it in another coroutine
driver.get('https://google.com')
trio.run(main)
A few things to note:
Fetch.requestPaused
works currently. Fetch.enable
sends the command and pauses requests, but it seems like selenium never receives the RequestPaused
event. (Which is why the code snippet is using Network.responseReceived
)devtools
class, but you can still view the source .py files for available attributes.Update:
Fetch.requestPaused
works now. Have a look at example: https://stackoverflow.com/a/75067388/20443541
It a also supports changing headers.
Upvotes: 11