2

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!

revy
  • 3,945
  • 7
  • 40
  • 85

1 Answers1

8

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:

  • Unfortunately, it doesn't seem like 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)
  • There is no documentation for the 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.

kaliiiiiiiii
  • 925
  • 1
  • 2
  • 21
Sentinent
  • 351
  • 2
  • 8
  • `in main async with driver.log._get_bidi_connection() as session: AttributeError: 'WebDriver' object has no attribute 'log' ` Got installed selenium v4.0.0rc1 Using pycharm also highlighting 'network' as `Unresolved attribute reference 'network' for class 'ModuleType'` at 'devtools.network' (2 usage) – void_eater Sep 20 '21 at 15:11
  • 1
    @void_eater Updated the answer to work with rc1. – Sentinent Sep 23 '21 at 20:27