[Selenium 4.0.0-beta-1]: how add event listeners in CDP
Asked Answered
F

1

3

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!

Franck answered 16/2, 2021 at 15:39 Comment(0)
A
9

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://mcmap.net/q/277102/-how-to-set-headers-in-python-selenium-chrome-webdriver It a also supports changing headers.

Apophyge answered 2/5, 2021 at 10:0 Comment(2)
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)Arcane
@Arcane Updated the answer to work with rc1.Apophyge

© 2022 - 2024 — McMap. All rights reserved.