1

I'm using Selenium to click a link in Firefox and I want it to download the file automatically. I have looked at various resources on the internet inc. Selenium firefox profile for saving a file. & Set Firefox profile to download files automatically using Selenium and Java. But I cannot get it to work. It just keep giving me a prompt with an open/save dialog. I am using Firefox 57.0.2, firefox driver 3.8.1, Geckodriver 0.19.1. I can manually set the firefox options to save the file but within the code i create a new profile so this obviously gets ignored. My code is:

System.setProperty(webdriver.gecko.driver",System.getProperty("user.dir") + Constants.GECKODRIVER);

FirefoxOptions firefoxOptions = new FirefoxOptions();
FirefoxProfile profile = new FirefoxProfile();

profile.setPreference("browser.download.dir", dirPath);
profile.setPreference("browser.download.folderList",2);
profile.setPreference("browser.helperApps.neverAsk.openFile","application/pdf,text/plain,application/octet-stream,application/x-pdf,application/vnd.pdf,application/vnd.openxmlformats-officedocument.spreadsheethtml,text/csv,text/html,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel");
profile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/pdf,text/plain,application/octet-stream,application/x-pdf,application/vnd.pdf,application/vnd.openxmlformats-officedocument.spreadsheethtml,text/csv,text/html,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel");
profile.setPreference("browser.helperApps.alwaysAsk.force, false);
profile.setPreference("browser.download.manager.useWindow, false);
profile.setPreference("browser.download.manager.focusWhenStarting, false);
profile.setPreference("browser.download.manager.alertOnEXEOpen, false);
profile.setPreference("browser.download.manager.showAlertOnComplete, false);
profile.setPreference("browser.download.manager.closeWhenDone, false);
profile.setPreference("browser.allowpopups, false);
profile.setPreference("pdfjs.disabled",true);
profile.setAcceptUntrustedCertificates(true);
profile.setAssumeUntrustedCertificatesIssuer(true);
firefoxOptions.setProfile(profile);
currentdriver = new FirefoxDriver();

I've even tried

profile.setPreference("plugin.disable_full_page_plugin_for_types","application/pdf,application/x-pdf");
profile.setPreference("plugin.scan.Acrobat,"99.0");

I have tried looking at the MIME types of the 2 files I need (.pdf and .xlsx) and I think they are text/html;charset=iso-8859-1.

Suggestions please?

rockOn123
  • 45
  • 1
  • 1
  • 7
  • First thing you need to do is to determine the exact mime of both files. Second you need to remove `browser.helperApps.neverAsk.openFile` since you cannot ask for the file to be opened and saved at the same time. – Florent B. Jan 29 '18 at 12:13
  • I have tried without .openFile but had no success. How can check the MIME type for sure (when not connected to internet). – rockOn123 Jan 29 '18 at 12:42
  • The mime is the content-type returned by the request. Have a look here: https://stackoverflow.com/questions/36309314/set-firefox-profile-to-download-files-automatically-using-selenium-and-java/36309735#36309735 – Florent B. Jan 29 '18 at 12:48
  • The latest version of firefox is slightly different to this post, however if I inspect the element and go to Network, the Type is x-unknown. – rockOn123 Jan 29 '18 at 13:46

2 Answers2

1

I too had no success using set_preference in webdriver.FirefoxProfile from selenium. What worked was a modification to the handlers.json file in the firefox profile directory.

The following had to be inserted under mimeTypes in the json file :

"application/text":{"action":0,"extensions":["csv"]},
dmdip
  • 1,665
  • 14
  • 15
0

yeah im not good at english, but i feel angry with this f***ing problem, so write this answer. i had no success with set_preference and json in the firefox profile directory, too. as you know, our life is short. all problems have to solved fastly. so i use ugly solution in below and f---ing solved.

i say again, our life is short. if this f***ing ugly solution is good to you, use this and feel free and enjoy our life.

from ahk import AHK
ahk = AHK()

sleep(2)
handle = ahk.win_get(title="ahk_class MozillaDialogClass") # yeah this is f***ing file download dialog window of firefox
if  ("" !=handle.title):
    handle.send("{Down}")
    sleep(0.2)
    handle.send("{Tab}{Tab}")
    sleep(0.2)
    handle.send("{Space}")
    sleep(0.2)
    handle.send("{Tab}{Enter}")
alfenmage
  • 1
  • 1