Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to grab headers in python selenium-webdriver

I am trying to grab the headers in selenium webdriver. Something similar to the following:

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

I need to use the Chrome webdriver because it supports flash and some other things that I need to test a web page. Here is what I have so far in 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('[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?

I have seen some other answers that recommend running an entire selenium server to get this information (https://github.com/derekargueta/selenium-profiler). How would I get it using something similar to the above with Webdriver?

like image 340
David542 Avatar asked Oct 05 '16 19:10

David542


People also ask

How do you get the header of a webpage in selenium?

Mastering XPath and CSS Selector for Selenium We can get the page title with Selenium webdriver. The method getTitle() is used to obtain the present page title and then we can get the result in the console.

How do I grab text in selenium?

We can get the text from a website using Selenium webdriver USING the getText method. It helps to obtain the text for a particular element which is visible or the inner text (which is not concealed from the page).


1 Answers

Unfortunately, you cannot get this information from the Selenium webdriver, nor will you be able to any time in the near future it seems. An excerpt from a very long conversation on the subject:

This feature isn't going to happen.

The gist of the main reason being, from what I gather from the discussion, that the webdriver is meant for "driving the browser", and extending the API beyond that primary goal will, in the opinion of the developers, cause the overall quality and reliability of the API to suffer.

One potential workaround that I have seen suggested in a number of places, including the conversation linked above, is to use BrowserMob Proxy, which can be used to capture HTTP content, and can be used with selenium - though the linked example does not use the Python selenium API. It does seem that there is a Python wrapper for BrowserMob Proxy, but I cannot vouch for it's efficacy since I have never used it.

like image 193
elethan Avatar answered Sep 19 '22 07:09

elethan