Ripundeep Gill
Ripundeep Gill

Reputation: 231

Control Firefox Download Prompt Using Selenium and Python

Here is my code :

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait

profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2);
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", "/home/ripundeep/Desktop/Python Challenges /")    
profile.set_preference("browser.helperApps.alwaysAsk.force", False)
profile.set_preference("browser.download.manager.alertOnEXEOpen", False)
profile.set_preference("browser.download.manager.focusWhenStarting", False)
profile.set_preference("browser.download.manager.useWindow", False)
profile.set_preference("browser.download.manager.showAlertOnComplete", False)
profile.set_preference("browser.download.manager.closeWhenDone", False)
profile.set_preference("browser.helperApps.neverAsk.openFile","text/csv")
profile.set_preference("browser.helperApps.neverAsk.saveToDisk","text/csv")
profile.update_preferences()

driver = webdriver.Firefox(firefox_profile=profile)
driver.get(url)
driver.find_element_by_css_selector("#id").send_keys("keyword")
WebDriverWait(driver, 1, poll_frequency=0.1).until(lambda drv:  len(drv.find_elements_by_css_selector("#ctl00_ContentPlaceHolder1_btnSubmit")) > 0)
driver.find_element_by_css_selector("#submitid").click()
driver.find_element_by_css_selector("#DownloadLinkId").click()

I want to stop firefox to show me download prompt and save it automatically and I have tried all possible solutions but didn't work. Please help.

Upvotes: 1

Views: 3249

Answers (2)

cruisepandey
cruisepandey

Reputation: 29382

You can possibly have this firefox profile, if you are using Python then you could this :

profile = FirefoxProfile()
profile.set_preference("browser.download.panel.shown", False)
profile.set_preference("browser.helperApps.neverAsk.openFile","text/csv,application/vnd.ms-excel")
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/msword, application/csv, application/ris, text/csv, image/png, application/pdf, text/html, text/plain, application/zip, application/x-zip, application/x-zip-compressed, application/download, application/octet-stream");
profile.set_preference("browser.download.manager.showWhenStarting", False);
profile.set_preference("browser.download.manager.alertOnEXEOpen", False);
profile.set_preference("browser.download.manager.focusWhenStarting", False);
profile.set_preference("browser.download.folderList", 2);
profile.set_preference("browser.download.useDownloadDir", True);
profile.set_preference("browser.helperApps.alwaysAsk.force", False);
profile.set_preference("browser.download.manager.alertOnEXEOpen", False);
profile.set_preference("browser.download.manager.closeWhenDone", True);
profile.set_preference("browser.download.manager.showAlertOnComplete", False);
profile.set_preference("browser.download.manager.useWindow", False);
profile.set_preference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", False);
profile.set_preference("pdfjs.disabled", True);
profile.set_preference("browser.download.dir", "C:\\Users\\***\\****\\Desktop\\Automation")
driver = webdriver.Firefox(firefox_profile = profile, executable_path = "Full file path to gecko driver.exe")

Upvotes: 0

alecxe
alecxe

Reputation: 474241

I remember providing more mime-type variants usually helped to solve issues like this:

mime_types = [
    'text/plain', 
    'application/vnd.ms-excel', 
    'text/csv', 
    'application/csv', 
    'text/comma-separated-values', 
    'application/download', 
    'application/octet-stream', 
    'binary/octet-stream', 
    'application/binary', 
    'application/x-unknown'
]
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", ",".join(mime_types))

I also think you should not be calling profile.update_preferences().


Aside from that, here are the steps to see what mime-type Firefox detects:

  • manually download the file with Firefox checking the "automatically save this file type" checkbox
  • open Help -> Troubleshooting Information
  • locate the "Profile Folder" button, click it
  • inside the profile folder locate the mimeTypes.rdf file
  • open the file in a text editor and look for the mimetypes mentioned there - the XML node attribute values that start with urn:mimetype
  • use the mimetypes you found in the browser.helperApps.neverAsk.saveToDisk comma-separated value

Upvotes: 4

Related Questions