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.
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' asUnresolved attribute reference 'network' for class 'ModuleType'
at 'devtools.network' (2 usage) – Arcane