Home > Software engineering >  Selenium set preferences to download excel file but dialog box pops up?
Selenium set preferences to download excel file but dialog box pops up?

Time:12-20

I am trying to download a excel file using selenium firefox.

def download_driver(location_for_download):
    profile = webdriver.FirefoxProfile()
    profile.set_preference("browser.download.folderList", 2)
    profile.set_preference("browser.download.manager.showWhenStarting", False)
    profile.set_preference("browser.download.dir", location_for_download)
    profile.set_preference("browser.helperApps.neverAsk.saveToDisk",
                           "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
    profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/csv")
    profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/plain")
    profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/comma-separated-values")
    profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/xls")
    driver = webdriver.Firefox(firefox_profile=profile)
    return driver

When I use this function in a script, the dialog box to save the file pops up. I am on ubuntu. I assume the MIME-type is wrong I have added all I could think of and search for.

The file in question shows up as a 2007 excel spreadsheet.

CodePudding user response:

Found the correct MIME type:

adding the following to the function makes it work:

profile.set_preference("browser.helperApps.neverAsk.saveToDisk",
                           "application/vnd.ms-excel application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")

Leaving the question up incase someone else comes looking.

CodePudding user response:

A bit of a different solution but works very reliably.

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);
  • Related