pythonseleniumselenium-webdriverbrowsermob-proxybrowsermob

Is there any way to capture selenium request headers with python?


I want to capture an authorization header from an outgoing request, either directly with selenium or through a proxy.

Methods I've tried:

  1. Getting request log with driver.get_log('performance') => Only some requests seem to be indexed, and none included the authorization header.
  2. Using browsermobproxy to intercept requests => Although all requests were logged, it returned no headers (headers==[], even though headersSize==814)

Here is the current code:

from time import sleep
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from browsermobproxy import Server

# Set configuration variables
browsermob_binary_path = r"path\to\browsermob-proxy"
facebook_credentials = {'email': 'my_email', 'password': 'my_password'}

# Configure proxy server
server = Server(browsermob_binary_path)
server.start()
proxy = server.create_proxy()

# Configure chrome to use proxy
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=%s' % proxy.proxy)
chrome_options.add_argument('--ignore-certificate-errors')

# Start chrome
driver = webdriver.Chrome(chrome_options=chrome_options)

# Start network capture
proxy.new_har('capture')

# Login to facebook
driver.get('https://apps.facebook.com/coin-master/?pid=socialfacebook')
driver.find_element_by_id("email").send_keys(facebook_credentials['email'])
driver.find_element_by_id("pass").send_keys(facebook_credentials['password'] + Keys.ENTER)

# Wait until game fully loads to make sure login request has taken place
sleep(100)

# Return all headers from captured requests
for i in range(len(proxy.har['log']['entries'])):
    print(proxy.har['log']['entries'][i]['request']['headers'])   # Always returns "[]"

# Close all dependencies
server.stop()
driver.quit()


Solution

  • The Solution

    To capture the headers in each request, I had to replace proxy.new_har('capture') with proxy.new_har('capture', options={'captureHeaders': True})

    Previously headers were ignored, but the captureHeaders flag forces browsermobproxy to capture them.