python : disable download popup when using firefox with selenium

Question:

I have script that using selenium and firefox to automating download action.
The problem is whenever I run script I always get pop up from firefox keep asking what kinds of action I would like to do, even though I set download path in firefox preference. I checked files and folders to create master mimeTypes.rdf for all users, but I couldn’t find mine.(I’m using ubuntu). I found ~/.mozilla/firefox but there was no file for directory of my profile name nor any file has an extension like .rdf

here is the criminal’s pic that making me crazy

firefox download popup

below is what I’ve done to disable the popup.

profile = FirefoxProfile()
profile.set_preference("browser.download.panel.shown", False)
profile.set_preference("browser.helperApps.neverAsk.openFile", 'application/zip')
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", 'application/zip')
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.dir", "/home/i-06/Downloads")
driver = webdriver.Firefox(firefox_profile=profile)
Asked By: ruth

||

Answers:

I doubt you need to define both. Remove the below line from your code

profile.set_preference("browser.helperApps.neverAsk.openFile", 'application/zip')

Also sometime the MIME type of zip file can be different based on the server. It could be any of below

  • application/octet-stream
  • multipart/x-zip
  • application/zip
  • application/zip-compressed
  • application/x-zip-compressed

So in Network tab check what is the content type you are getting and add that to your profile to make sure the dialog doesn’t come

Answered By: Tarun Lalwani

I have spent many hours trying to suppress that “save or open” pop-up that appears when downloading a file using the firefox driver with selenium (python 3.x). None of the many suggestions involving various values for profile.set_preference worked for me. Maybe I missed something.

Still, I finally got it working by the other method that is recommended : using an existing firefox profile.

You can tweak your default (or custom) profile to the file save behaviour you want. Type the following in the firefox address bar and make changes here :

about:preferences#applications

Then the only setting up you need to do to download the file into your current working directory is :

from selenium import webdriver
fp = webdriver.FirefoxProfile(<your firefox profile directory>)
fp.set_preference("browser.download.folderList",2)
fp.set_preference("browser.download.dir", os.getcwd())
driver = webdriver.Firefox(firefox_profile=fp)

If you have a typical ubuntu setup, you can find your default firefox profile dir by viewing ~/.mozilla/firefox/profile.ini

In that .ini file, look for Path under [Profile0]

Answered By: dmdip

I removed profile.set_preference("browser.helperApps.neverAsk.openFile", 'application/zip') as Tarun Lalwani suggest and it still work. But my problem was that I put application/mp4 instead of video/mp4. You could check MIME type here.

Answered By: slimBA