Skip to content

Instantly share code, notes, and snippets.

@cgoldberg
Created October 13, 2025 14:26
Show Gist options
  • Save cgoldberg/cc9997778db4ff004d2058adb1a9d9f8 to your computer and use it in GitHub Desktop.
Save cgoldberg/cc9997778db4ff004d2058adb1a9d9f8 to your computer and use it in GitHub Desktop.
Python - Selenium BiDi - Network Request Logging
# Selenium BiDi - Network Request Logging
# - launch Chrome
# - add a request handler that logs all HTTP headers from reqests made during page load
# - navigate to a web page
# - remove request handler
# - quit Chrome
import pprint
from selenium import webdriver
from selenium.webdriver.common.bidi.browsing_context import ReadinessState
URL = "https://example.com"
# callback that logs headers
def log_requests(request):
print(f"Intercepted URL: {request.url}")
print("Headers:")
pprint.pprint(request.headers)
request.continue_request()
options = webdriver.ChromeOptions()
options.enable_bidi = True
driver = webdriver.Chrome(options=options)
callback_id = driver.network.add_request_handler("before_request", log_requests)
driver.browsing_context.navigate(url=URL, context=driver.current_window_handle)
driver.network.remove_request_handler("before_request", callback_id)
driver.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment