Selenium Wire

5.1.0 · active · verified Sat Apr 11

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+.

Warnings

Install

Imports

Quickstart

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()

view raw JSON →