2

I know this question has been asked before but after trying suggestions, I am constantly getting the OS download file window. What I am trying to do is download a pdf file. I have set the browser preferences but despite that fact, it does not suppress the OS window.

Here is the code snippet that I have written:

firefoxProfile = webdriver.FirefoxProfile()
firefoxProfile.set_preference('browser.download.folderList', 2)
firefoxProfile.set_preference('browser.download.manager.showWhenStarting', False)
firefoxProfile.set_preference('browser.download.dir', '/media/pinku/Pinku')
firefoxProfile.set_preference('browser.helperApps.alwaysAsk.force', False)
firefoxProfile.set_preference('browser.helperApps.neverAsk.saveToDisk',
'application/octet-stream')

self.driver = webdriver.Firefox(firefoxProfile)

I am using Ubuntu 12.10, Firefox, webdriver, python

Praveen Pandey
  • 658
  • 3
  • 16
  • 32

3 Answers3

3

I think you might have gotten the MIME type wrong. Try this

firefoxProfile.set_preference('browser.helperApps.neverAsk.saveToDisk',
'application/pdf,application/x-pdf')

A discussion about pdf MIME types can be found here You should check the mime type tht your firefox sees when you try to download the pdf. It might me set wrongly by the server!

Side note: Whenever this topic comes up (downloading files via selenium webdriver) I strongly advise against doing it at all! Have a read through the article "How To Download Files With Selenium And Why You Shouldn’t" for a reasoning. Basically it suggests to use other means to test direct downloads.

Update: I did not put both mime types in one string before which was wrong. Also I added the suggestion about checking what the server actually delivers.

Community
  • 1
  • 1
luksch
  • 11,497
  • 6
  • 38
  • 53
  • The article you suggested is great because it gives me an alternative way of testing file download. However, I am still surprised as to why my code above is not working? I changed the mime type to application/x-pdf and even tried application/pdf but it does not work! Still shows the OS window.. :( – Praveen Pandey Sep 22 '13 at 11:20
  • sorry to hear that it was not the MIME type after all. You could try setting ```browser.download.folderList', 1``` maybe, but that is a wild guess – luksch Sep 22 '13 at 11:49
  • Even tried with `browser.download.folderList', 1`. Doesn't work. – Praveen Pandey Sep 23 '13 at 16:56
  • did you check the mime type that is actually returned by the server you try to get the pdf from? – luksch Sep 23 '13 at 17:24
  • I used Chrome browser to find out the answer to your question and under the **Network** tab of Chrome's developer's tool (), I see **application/pdf**. Is this the MIME type you are talking about? Sorry to be a little naive here. – Praveen Pandey Sep 25 '13 at 05:15
  • yes, that is the one. you tell firefox through the prefs that you do not want to start a pdf-viewer automatically but save to disk by ```firefoxProfile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/pdf')``` It seems you got indeed yet another problem. Sorry, but I really don't know what else could be the matter. – luksch Sep 25 '13 at 07:56
  • Indeed that is what I have already tried. No problem. Thanks for your attempt to solve my issue. – Praveen Pandey Sep 25 '13 at 13:50
  • The web server was sending the wrong mime type for PDFs. Try `binary/octet-stream` and if that doesn't work, diff the mimeTypes.rdf in your current Firefox profile before and after answering the download popup. https://stackoverflow.com/questions/34779013/issue-with-mime-type-when-setting-preferences-in-firefox-with-selenium – Colin Jul 18 '17 at 00:52
2

I have been working with firefox 24.03 (this is the ESR version) This version of firefox introduced pdfjs. This opens the PDF in browser.

So you need to supress that. Here is the code/firefox profile that worked for me.

fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.folderList",2)
fp.set_preference("browser.download.manager.showWhenStarting",False)
fp.set_preference("browser.download.dir","C:\\temp")
fp.set_preference("browser.helperApps.neverAsk.saveToDisk","application/pdf")
fp.set_preference("plugin.disable_full_page_plugin_for_types", "application/pdf")
fp.set_preference("pdfjs.disabled", True)
driver = webdriver.Firefox(firefox_profile=fp)

With this profile all my pdf downloads go to "C:\temp"

Nish
  • 189
  • 2
  • 6
0

I had a similar problem because the mime type returned by the server was "text/plain" instead of "text/csv".

This is what worked for me (using watir-webdriver):

profile = Selenium::WebDriver::Firefox::Profile.new
profile['browser.download.folderList'] = 2 # custom location
profile['browser.download.dir'] = download_directory
profile['browser.helperApps.neverAsk.saveToDisk'] = "text/plain"
browser = Watir::Browser.new :firefox, :profile => profile

More info on downloading with watir-webdriver here: http://watirwebdriver.com/browser-downloads/

genegc
  • 1,630
  • 18
  • 16