如何在python硒中获取标题

我试图抓住硒webdriver头。 类似于以下内容:

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

我需要使用Chrome程序,因为它支持Flash和其他一些我需要测试网页的东西。 这是我迄今为止在硒中所做的:

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?

我见过一些其他的建议,运行整个硒服务器来获取这些信息(https://github.com/derekargueta/selenium-profiler)。 如何用Webdriver使用类似于上面的内容来获得它?


不幸的是,你无法从Selenium的网络驱动器获得这些信息,也不会在不久的将来任何时候看到它。 关于这个问题的很长一段话的摘录:

此功能不会发生。

主要原因的主要依据是,从我从讨论中得到的结果来看,webdriver意味着“驱动浏览器”,并且将API扩展超出主要目标,开发人员认为,会导致整体质量和API的可靠性受到影响。

我在一些地方(包括上面链接的对话)中看到的一种潜在解决方法是使用BrowserMob代理,它可以用于捕获HTTP内容,并可以与硒一起使用 - 虽然链接的示例不使用Python selenium API。 它看起来似乎有一个BrowserMob Proxy的Python包装器,但是我从来没有使用它,所以我不能担保它的功效。


您可以尝试Mobilenium,一个绑定BrowserMob Proxy和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'

您可以通过日志获得标题(来自Mma的回答)

from selenium import webdriver
import json
driver = webdriver.PhantomJS(executable_path=r"your_path")
har = json.loads(driver.get_log('har')[0]['message']) # get the log
print('headers: ', har['log']['entries'][0]['request']['headers'])
链接地址: http://www.djcxy.com/p/93577.html

上一篇: How to grab headers in python selenium

下一篇: Get Service instance via console