5

I use python 2.7 and the selenium driver as downloaded from

pip install selenium

How can I get the http headers from a web request.

In particular I click a button/link and the server replies with a response containing a csv file.

It would be awesome if I could get the filename from the http headers.

Another option would e to access the browser's download history.

Any ideas how the above can be achieved?

1
  • Any particular reason you want to do this? Are you trying to verify the web is making a correct API call?
    – Greg
    Nov 17, 2014 at 7:44

1 Answer 1

4

Selenium can't actually do this (capture network traffic). I would suggest using a third party tool like Browser Mob

I don't know if you can get your browser's download history... but as a workaround you can just download files to an empty directory, and just call that your download history. You could also rank files by time downloaded using os.path.getmtime

import os

from selenium import webdriver

fp = webdriver.FirefoxProfile()

fp.set_preference("browser.download.folderList",2)
fp.set_preference("browser.download.manager.showWhenStarting",False)
fp.set_preference("browser.download.dir", "/tmp/empty-dir")
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream")

browser = webdriver.Firefox(firefox_profile=fp)
browser.get("http://pypi.python.org/pypi/selenium")
browser.find_element_by_partial_link_text("selenium-2").click()

os.listdir("/tmp/empty-dir")
['selenium-2.44.0.tar.gz']
2
  • Is getting the download history possible?
    – gosom
    Nov 17, 2014 at 8:29
  • 2
    I can't find any reference to the download history in the source or the docs. That doesn't mean you couldn't do it; but it probably isnt trivial
    – Greg
    Nov 17, 2014 at 9:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.