0

Auto download is not working in firefox. Please help me with below code:

FirefoxOptions options = new FirefoxOptions();         
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.folderList",2); //Use for the default download directory the last folder specified for a download
profile.setPreference("browser.download.dir", "D:");
profile.setPreference("browser.download.useDownloadDir", true);       profile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
profile.setPreference( "browser.download.manager.showWhenStarting", false );
profile.setPreference("pdfjs.disabled", true);  // disable the built-in PDF viewer
options.setProfile(profile);
WebDriver driver = new FirefoxDriver(options); 
Sers
  • 12,047
  • 2
  • 12
  • 31
  • A dublicate question https://stackoverflow.com/questions/51949871/selenium-download-file-automatically-c-sharp/51953426#51953426 check my answer there – Sers Aug 21 '18 at 20:12
  • I will change MIME according to xlsx. But what about options.addPreference("pdfjs.enabledCache.state",false) – Rajat Prajapati Aug 21 '18 at 20:17
  • it is for pdf if need – Sers Aug 21 '18 at 20:18
  • So in case of xlsx should I ignore it – Rajat Prajapati Aug 21 '18 at 20:19
  • Possible duplicate of [How to handle download pop-up in firefox, while downloading excel using Selenium Webdriver](https://stackoverflow.com/questions/36355204/how-to-handle-download-pop-up-in-firefox-while-downloading-excel-using-selenium) – Sers Aug 21 '18 at 20:28
  • In my case when I click "download excel" button, the request gets disappeared immediately from network window. In short its giving me no time to see content type. – Rajat Prajapati Aug 22 '18 at 04:06

2 Answers2

0

For excel try mime:

FirefoxOptions options = new FirefoxOptions();
options.addPreference("browser.download.folderList", 2)
        .addPreference("browser.download.dir", downloadPath)
        .addPreference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.ms-excel,text/xml,application/x-excel,application/x-msexcel,application/xhtml+xml,application/octet-stream,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

WebDriver driver = new FirefoxDriver(options);

How to handle download pop-up in firefox, while downloading excel using Selenium Webdriver

Sers
  • 12,047
  • 2
  • 12
  • 31
  • For xlsx this should be MIME – Rajat Prajapati Aug 21 '18 at 20:28
  • application/vnd.openxmlformats-officedocument.spreadsheetml.sheet – Rajat Prajapati Aug 21 '18 at 20:28
  • add `application/vnd.openxmlformats-officedocument.spreadsheetml.sheet` – Sers Aug 21 '18 at 20:29
  • I tried your code. however still no luck,its not working. – Rajat Prajapati Aug 22 '18 at 03:45
  • FirefoxOptions options = new FirefoxOptions(); options.addPreference("browser.download.folderList", 2); options.addPreference("browser.download.dir", "D:\\Downloads"); options.addPreference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; application/vnd.ms-excel; text/xml;application/x-excel; application/x-msexcel"); WebDriver driver = new FirefoxDriver(options); – Rajat Prajapati Aug 22 '18 at 03:45
  • You have to find mime type somehow. Try to add `application/xhtml+xml,application/octet-stream` – Sers Aug 22 '18 at 07:06
  • Thanks, after adding above MIME its working.Is there any way to get MIME of a file in one go ?? – Rajat Prajapati Aug 22 '18 at 07:37
  • Ok. I updated my answer for other people. You can close than – Sers Aug 22 '18 at 07:39
0

to be more precise MIME that worked for me for automating download of xlsx file through firefox is "application/octet-stream".