如何在Selenium中获取请求标头 [英] How to get request headers in Selenium

查看:180
本文介绍了如何在Selenium中获取请求标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

https://www.sahibinden.com/en

如果您打开隐身窗口并在Fiddler中检查标题,那么您将获得以下两个主要标题:

If you open it incognito window and check headers in Fiddler then these are the two main headers you get:

当我单击最后一个并检查请求标头时,这就是我得到的

When I click the last one and check request headers this is what I get

我想在Python中获得这些标头.有什么办法可以使用Selenium来获得这些东西吗?我在这里有点无能为力.

I want to get these headers in Python. Is there any way that I can get these using Selenium? Im a bit clueless here.

推荐答案

您可以使用Selenium Wire.这是为此目的而开发的Selenium扩展程序.

You can use Selenium Wire. It is a Selenium extension which has been developed for this exact purpose.

https://pypi.org/project/selenium-wire/

pip安装后的示例:

An example after pip install:

##  Import webdriver from Selenium Wire instead of Selenium
from seleniumwire import webdriver

##  Get the URL
driver = webdriver.Chrome("my/path/to/driver", options=options)
driver.get("https://my.test.url.com")

##  Print request headers
for request in driver.requests:
  print(request.url) # <--------------- Request url
  print(request.headers) # <----------- Request headers
  print(request.response.headers) # <-- Response headers

这篇关于如何在Selenium中获取请求标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆