2016-10-05 50 views
6

我想要在selenium webdriver中獲取標題。一些類似以下內容:如何在python selenium-webdriver中獲取標題

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

我需要,因爲它支持Flash和我需要測試一個網頁一些其他的事情要使用Chrome的webdriver。以下是我迄今爲止在硒:

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('[email protected]') 
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使用類似於上面的內容來獲得它?

+0

能否請您詳細說明你想提取和什麼什麼頭?謝謝。 – alecxe

+0

很確定你不能開箱即用。 –

回答

5

不幸的是,你不能從Selenium網絡驅動程序獲取此信息,也不會在任何時候在不久的將來似乎。摘自a very long conversation on the subject

此功能不會發生。

從我從討論中得到的結果來看,主要原因的主旨是webdriver是用於「驅動瀏覽器」,並且將API擴展到超出主要目標的範圍,開發人員認爲,導致API的整體質量和可靠性受到影響。

在許多地方(包括上面鏈接的對話)中提到的一種潛在解決方法是使用BrowserMob Proxy(可用於捕獲HTTP內容)和can be used with selenium(儘管鏈接的示例不使用Python硒API。它似乎有a Python wrapper for BrowserMob Proxy,但我不能保證它的功效,因爲我從來沒有使用它。

+0

如何執行JavaScript或在頁面內的東西登錄到控制檯或什麼?有沒有一種(hackish)的方式來做類似的事情? – David542

+0

我在這個主題上反覆看到的一個建議是使用BrowserMob Proxy:https://github.com/lightbody/browsermob-proxy,它可以與selenium一起使用:https://github.com/lightbody/browsermob-proxy #使用與 - 硒。但是,我沒有這個實用工具的經驗。抱歉,我無法提供更多幫助! – elethan

+0

@ David542也看到我更新的答案的最後一段。它包含一個指向BrowserMob Proxy的Python包裝的鏈接,可能適用於您的用例。 – elethan

-2

你是指HTTP頭數據,對不對?這不是Selenium的範圍:Selenium automates browsers. That's it!所以如果你不能用你的瀏覽器來做(而且我不知道有什麼方法),Selenium是錯誤的工具。但是,如果您可以使用JavaScript,則可以使用driver.execute_script(script, *args),如here所述。

1

你可以嘗試Mobilenium(https://github.com/rafpyprog/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' 
0

你可以通過日誌的標題(來源從Mma's answer

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'])