{"id":4253,"library":"selenium-wire","title":"Selenium Wire","description":"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+.","status":"active","version":"5.1.0","language":"en","source_language":"en","source_url":"https://github.com/wkeeling/selenium-wire","tags":["selenium","web scraping","network interception","proxy","webdriver","automation"],"install":[{"cmd":"pip install selenium-wire","lang":"bash","label":"Install Selenium Wire"}],"dependencies":[{"reason":"Core dependency for browser automation functionality.","package":"selenium"},{"reason":"Required for HTTPS decryption on non-Windows operating systems.","package":"pyOpenSSL","optional":true}],"imports":[{"note":"Importing `webdriver` from `seleniumwire` is essential to enable network interception and other extended features. Importing from `selenium` will result in a standard Selenium WebDriver without Selenium Wire's enhancements.","wrong":"from selenium import webdriver","symbol":"webdriver","correct":"from seleniumwire import webdriver"},{"note":"Standard Selenium Options classes are used for browser-specific configurations, which can then be passed to `seleniumwire.webdriver.Chrome()` or `Firefox()`.","symbol":"ChromeOptions","correct":"from selenium.webdriver.chrome.options import Options"}],"quickstart":{"code":"import os\nfrom seleniumwire import webdriver\nfrom selenium.webdriver.chrome.options import Options\n\n# Configure Chrome options (e.g., headless mode)\nchrome_options = Options()\n# chrome_options.add_argument('--headless')\n\n# Configure Selenium Wire options (e.g., disable capture to start)\n# For proxy with authentication, use env vars or direct string:\n# PROXY_HOST = os.environ.get('PROXY_HOST', 'your_proxy_host')\n# PROXY_PORT = os.environ.get('PROXY_PORT', '8080')\n# PROXY_USER = os.environ.get('PROXY_USER', 'your_proxy_username')\n# PROXY_PASS = os.environ.get('PROXY_PASS', 'your_proxy_password')\n# seleniumwire_options = {\n#     'proxy': {\n#         'http': f'http://{PROXY_USER}:{PROXY_PASS}@{PROXY_HOST}:{PROXY_PORT}',\n#         'https': f'https://{PROXY_USER}:{PROXY_PASS}@{PROXY_HOST}:{PROXY_PORT}',\n#         'no_proxy': 'localhost,127.0.0.1'\n#     }\n# }\n\n# Create a new instance of the Chrome driver with Selenium Wire\ndriver = webdriver.Chrome(options=chrome_options, seleniumwire_options={'disable_capture': False})\n\ntry:\n    # Go to a webpage\n    driver.get('https://www.google.com')\n\n    # Access requests via the `requests` attribute\n    print('Captured Requests:')\n    for request in driver.requests:\n        if request.response:\n            print(f'URL: {request.url}, Status: {request.response.status_code}, Content-Type: {request.response.headers.get(\"Content-Type\")}')\n\n    # Example: Intercept a request to add a header\n    def interceptor(request):\n        if request.url == 'https://www.google.com/':\n            request.headers['X-Custom-Header'] = 'SeleniumWireTest'\n\n    driver.request_interceptor = interceptor\n    driver.get('https://httpbin.org/headers') # Navigate to a site that echoes headers\n    print('\\nIntercepted Request to httpbin.org/headers:')\n    for request in driver.requests:\n        if 'httpbin.org/headers' in request.url and request.response:\n            print(f'URL: {request.url}, Status: {request.response.status_code}')\n            # For demonstration, typically you'd parse response.body to confirm header\n            print('Response body snippet (may contain X-Custom-Header if successfully applied):')\n            try:\n                print(request.response.body.decode('utf-8')[:200] + '...')\n            except Exception as e:\n                print(f'Could not decode response body: {e}')\n\nfinally:\n    # Close the browser\n    driver.quit()","lang":"python","description":"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`."},"warnings":[{"fix":"Update your code to use `request.url` for full URLs and expect `b''` for empty bodies.","message":"In 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`.","severity":"breaking","affected_versions":">=5.0.0"},{"fix":"Pass proxy settings within the `seleniumwire_options` dictionary, e.g., `driver = webdriver.Chrome(seleniumwire_options={'proxy': {'http': 'http://user:pass@host:port'}})`.","message":"Selenium 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.","severity":"gotcha","affected_versions":"All"},{"fix":"Ensure OpenSSL is installed on your system. On Debian/Ubuntu: `sudo apt-get install openssl`. On macOS: `brew install openssl`.","message":"For 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.","severity":"gotcha","affected_versions":"All"},{"fix":"If encountering this error, try downgrading `blinker`: `pip install blinker==1.7.0`.","message":"There have been reports of `ModuleNotFoundError: No module named blinker._saferef` with certain `blinker` versions. This dependency conflict can prevent Selenium Wire from starting.","severity":"gotcha","affected_versions":"Potentially specific combinations with blinker > 1.7.0"},{"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.","message":"Request 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.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}