如何在python selenium-webdriver中抓取头文件

Dav*_*542 11 python selenium

我试图抓住selenium webdriver中的标题.类似于以下内容:

>>> import requests
>>> res=requests.get('http://google.com')
>>> print res.headers
Run Code Online (Sandbox Code Playgroud)

我需要使用Chromewebdriver,因为它支持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?
Run Code Online (Sandbox Code Playgroud)

我已经看到一些其他答案建议运行整个selenium服务器来获取此信息(https://github.com/derekargueta/selenium-profiler).如何使用Webdriver使用与上面类似的东西?

ele*_*han 9

不幸的是,您无法从Selenium webdriver获取此信息,也不会在不久的将来任何时候看到它.关于这个主题的长时间谈话的摘录:

此功能不会发生.

主要原因是的要点,从我从讨论中收集,该webdriver的是为"驱动器",并延伸超出主要目标的API会,在开发商看来,导致整体质量和API的可靠性受到影响.

我在许多地方看到的一个潜在的解决方法,包括上面链接的对话,是使用BrowserMob Proxy,它可以用来捕获HTTP内容,并且可以与selenium一起使用 - 尽管链接的示例不使用Python selenium API.看起来似乎有一个用于BrowserMob Proxy的Python包装器,但我不能保证它的功效,因为我从未使用它.


mun*_*ish 6

现在,我想这很容易https://pypi.org/project/selenium-wire/ 它是 selenium 的扩展。使用from seleniumwire import webdriver并照常进行。


Raf*_*iro 5

您可以尝试Mobilenium,这是一个绑定 BrowserMob 代理和 Selenium 的 Python 包(仍在开发中)。

一个使用示例:

>>> from mobilenium import mobidriver
>>>
>>> browsermob_path = 'path/to/browsermob-proxy'
>>> mob = mobidriver.Firefox(browsermob_binary=browsermob_path)
>>> mob.get('http://python-requests.org')
301
>>> mob.response['redirectURL']
'http://docs.python-requests.org'
>>> mob.headers['Content-Type']
'application/json; charset=utf8'
>>> mob.title
'Requests: HTTP for Humans \u2014 Requests 2.13.0 documentation'
>>> mob.find_elements_by_tag_name('strong')[1].text
'Behold, the power of Requests'
Run Code Online (Sandbox Code Playgroud)