Registry / http-networking / selenium-wire

selenium-wire

JSON →
library5.1.0pypypi✓ verified 23d ago

Selenium Wire is a lightweight Python package that extends Selenium's WebDriver functionality, providing advanced capabilities to access and manipulate underlying network requests made by the browser. It acts as a proxy server, intercepting and logging all HTTP/HTTPS traffic in real time. Developers can use its APIs to monitor, modify, inspect, or block requests and responses during web automation and scraping. The library is currently at version 5.1.0 and is actively used and maintained, compatible with Python 3.7+ and Selenium 4.0.0+.

pip install selenium-wire
INSTALL
IMPORT
SIG · SELENIUM-WIRE
S
selenium-wire
http-networkingpythonv5.1.0
Install
5.7s avg
Import
Disk
100MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v5.1.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 95.4MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 5.7s · import 0.000s · 105MB
100MB installed
● package 100MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

webdriver
import seleniumwire
from seleniumwire import webdriver

This quickstart demonstrates how to initialize a Selenium Wire WebDriver for Chrome, navigate to a page, and then iterate through the captured network requests and their responses. It also includes an example of setting a request interceptor to modify headers for a specific URL, showing how to gain programmatic control over network traffic. Remember to have a compatible browser driver (e.g., ChromeDriver) installed and accessible in your system PATH or specified via `executable_path`.

import os from seleniumwire import webdriver from selenium.webdriver.chrome.options import Options # Configure Chrome options (e.g., headless mode) chrome_options = Options() # chrome_options.add_argument('--headless') # Configure Selenium Wire options (e.g., disable capture to start) # For proxy with authentication, use env vars or direct string: # PROXY_HOST = os.environ.get('PROXY_HOST', 'your_proxy_host') # PROXY_PORT = os.environ.get('PROXY_PORT', '8080') # PROXY_USER = os.environ.get('PROXY_USER', 'your_proxy_username') # PROXY_PASS = os.environ.get('PROXY_PASS', 'your_proxy_password') # seleniumwire_options = { # 'proxy': { # 'http': f'http://{PROXY_USER}:{PROXY_PASS}@{PROXY_HOST}:{PROXY_PORT}', # 'https': f'https://{PROXY_USER}:{PROXY_PASS}@{PROXY_HOST}:{PROXY_PORT}', # 'no_proxy': 'localhost,127.0.0.1' # } # } # Create a new instance of the Chrome driver with Selenium Wire driver = webdriver.Chrome(options=chrome_options, seleniumwire_options={'disable_capture': False}) try: # Go to a webpage driver.get('https://www.google.com') # Access requests via the `requests` attribute print('Captured Requests:') for request in driver.requests: if request.response: print(f'URL: {request.url}, Status: {request.response.status_code}, Content-Type: {request.response.headers.get("Content-Type")}') # Example: Intercept a request to add a header def interceptor(request): if request.url == 'https://www.google.com/': request.headers['X-Custom-Header'] = 'SeleniumWireTest' driver.request_interceptor = interceptor driver.get('https://httpbin.org/headers') # Navigate to a site that echoes headers print('\nIntercepted Request to httpbin.org/headers:') for request in driver.requests: if 'httpbin.org/headers' in request.url and request.response: print(f'URL: {request.url}, Status: {request.response.status_code}') # For demonstration, typically you'd parse response.body to confirm header print('Response body snippet (may contain X-Custom-Header if successfully applied):') try: print(request.response.body.decode('utf-8')[:200] + '...') except Exception as e: print(f'Could not decode response body: {e}') finally: # Close the browser driver.quit()
Debug
Known issues
breakingIn version 5.0.0, the `request.path` attribute changed to return only the path segment of the URL. To retrieve the full URL, use `request.url` instead. Additionally, empty request and response bodies are now returned as empty bytes (`b''`) rather than `None`.
fix
Update your code to use `request.url` for full URLs and expect `b''` for empty bodies.
affects: >=5.0.0
gotchaSelenium Wire uses its own proxy server for request interception. Therefore, you cannot configure proxies directly via Selenium's `DesiredCapabilities` or `Options` objects (e.g., `chrome_options.add_argument('--proxy-server=...')`). Instead, all proxy configurations must be passed through the `seleniumwire_options` dictionary when initialising the WebDriver.
fix
Pass proxy settings within the `seleniumwire_options` dictionary, e.g., `driver = webdriver.Chrome(seleniumwire_options={'proxy': {'http': 'http://user:pass@host:port'}})`.
affects: All
gotchaFor HTTPS request decryption and interception on Linux and macOS, OpenSSL is a required dependency. While often pre-installed, its absence will prevent Selenium Wire from capturing HTTPS traffic effectively.
fix
Ensure OpenSSL is installed on your system. On Debian/Ubuntu: `sudo apt-get install openssl`. On macOS: `brew install openssl`.
affects: All
gotchaThere have been reports of `ModuleNotFoundError: No module named blinker._saferef` with certain `blinker` versions. This dependency conflict can prevent Selenium Wire from starting.
fix
If encountering this error, try downgrading `blinker`: `pip install blinker==1.7.0`.
affects: Potentially specific combinations with blinker > 1.7.0
gotchaRequest and response capture is asynchronous. If you access `driver.requests` immediately after a page load or action, some requests or their responses might not yet be fully captured. You might need to implement explicit or implicit waits.
fix
Use Selenium's explicit waits (e.g., `WebDriverWait`) to wait for specific requests to appear or for the DOM to update, implying that network activity has settled. Alternatively, use `driver.wait_for_request()` for specific request patterns.
affects: All
Upgrade
Version history
5.1.0latest on PyPI · released Oct 15, 2022
Audit
Dependencies
seleniumrequiredCore dependency for browser automation functionality.
pyOpenSSLoptionalRequired for HTTPS decryption on non-Windows operating systems.
Agent activity
21 hits · last 30 days
node
16
Amazon
1
OpenAI (training)
1
Resources
selenium-wire — pip install selenium-wire · libregistry