Created
October 13, 2025 14:26
-
-
Save cgoldberg/cc9997778db4ff004d2058adb1a9d9f8 to your computer and use it in GitHub Desktop.
Python - Selenium BiDi - Network Request Logging
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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