Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium get http response headers or access the browsers's download history

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?

like image 838
gosom Avatar asked Sep 29 '22 09:09

gosom


1 Answers

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']
like image 78
Greg Avatar answered Oct 04 '22 02:10

Greg