【问题标题】:Is there any way to capture selenium request headers with python?有没有办法用 python 捕获 selenium 请求标头?
【发布时间】:2020-08-05 16:58:08
【问题描述】:

我想直接使用 selenium 或通过代理从传出请求中捕获授权标头。

我尝试过的方法:

  1. 使用driver.get_log('performance') 获取请求日志 => 似乎只有一些请求被编入索引,并且没有一个包含授权标头。
  2. 使用 browsermobproxy 拦截请求 => 尽管记录了所有请求,但它没有返回任何标头(headers==[],即使 headersSize==814

这是当前代码:

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

【问题讨论】:

    标签: python selenium selenium-webdriver browsermob-proxy browsermob


    【解决方案1】:

    解决方案

    要捕获每个请求中的标头,我必须替换 proxy.new_har('capture')proxy.new_har('capture', options={'captureHeaders': True})

    以前的标头被忽略,但 captureHeaders 标志强制 browsermobproxy 捕获它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-21
      • 1970-01-01
      • 2010-11-01
      • 1970-01-01
      • 2020-09-20
      • 2014-06-09
      • 2011-11-10
      相关资源
      最近更新 更多