小编典典

如何在python selenium-webdriver中获取标头

selenium

我正在尝试获取Selenium Webdriver中的标题。类似于以下内容:

>>> import requests
>>> res=requests.get('http://google.com')
>>> print res.headers

我需要使用网络驱动Chrome程序,因为它支持Flash和测试网页所需的其他功能。这是我到目前为止在Selenium中拥有的东西:

from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://login.comcast.net/login?r=comcast.net&s=oauth&continue=https%3A%2F%2Flogin.comcast.net%2Foauth%2Fauthorize%3Fclient_id%3Dxtv-account-selector%26redirect_uri%3Dhttps%3A%2F%2Fxtv-pil.xfinity.com%2Fxtv-authn%2Fxfinity-cb%26response_type%3Dcode%26scope%3Dopenid%2520https%3A%2F%2Flogin.comcast.net%2Fapi%2Flogin%26state%3Dhttps%3A%2F%2Ftv.xfinity.com%2Fpartner-success.html%26prompt%3Dlogin%26response%3D1&reqId=18737431-624b-44cb-adf0-2a85d91bd662&forceAuthn=1&client_id=xtv-account-selector')
driver.find_element_by_css_selector('#user').send_keys('XY@comcast.net')
driver.find_element_by_css_selector('#passwd').send_keys('XXY')
driver.find_element_by_css_selector('#passwd').submit()
print driver.headers ### How to do this?

我还看到了一些其他建议,建议运行整个selenium服务器以获取此信息(https://github.com/derekargueta/selenium-
profiler)。我如何使用与上述类似的Webdriver来获得它?


阅读 405

收藏
2020-06-26

共1个答案

小编典典

不幸的是,你 不能
得到硒webdriver的这些信息,你也将能够在不久的将来,似乎任何时候。一段关于该主题的长时间对话的摘录:

此功能将不会发生。

根据我从讨论中得出的结论,主要原因在于,WebDriver旨在“驱动浏览器”,并且将API扩展到该主要目标之外,这对开发人员而言将导致整体质量和API的可靠性受到影响。

我在许多地方都看到过建议的一种可能的解决方法,包括上面链接的对话,是使用BrowserMob
Proxy
,该代理可用于捕获HTTP内容,并且可与硒一起使用 -尽管链接的示例未使用Python硒API。似乎确实有一个适用于BrowserMob
Proxy的Python包装器
,但是由于我从未使用过它,因此我不能保证它的功效。

2020-06-26