0

I'm trying to use code which I found on Stackoverflow in other topics to do not ask user about action which should be taken to download PDF file.

Code which I'm using is:

 FirefoxProfile specialProfile = new FirefoxProfile();
 specialProfile.SetPreference("browser.helperApps.alwaysAsk.force", false);
 specialProfile.SetPreference("browser.download.manager.showWhenStarting", false);
 specialProfile.SetPreference("browser.download.folderList", 2);
 specialProfile.SetPreference("browser.download.dir", Setup.DownloadContractPath);            
 specialProfile.SetPreference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", false);
 specialProfile.SetPreference("browser.download.useDownloadDir", true);

 specialProfile.SetPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");

 return new FirefoxDriver(specialProfile);

But it seems that it's not working in my case. File I'm downloading is the type: PDF And even if i'm using the code above i got the following screen:

enter image description here

Does anyone know what may i do wrong?

Yi Zeng
  • 32,020
  • 13
  • 97
  • 125
ravenik
  • 852
  • 3
  • 9
  • 26
  • an alternative solution if you are working on windows is to use sendkeys and hit tab-tab and in order to click the OK button – Xwris Stoixeia Apr 11 '13 at 15:05
  • Gah, why are you downloading something anyway? Are you actually doing anything with it? – Ardesco Apr 12 '13 at 14:57
  • Hi, actually there was another process in the application which has been started on finish downloading the file. That's why i needed to get it. – ravenik Apr 23 '13 at 06:49

2 Answers2

2

What Firefox and Selenium version are you using?

I'm using Firefox 20.0, the default behaviour is to preview the pdf files rather than downloading, so I need to add specialProfile.SetPreference("pdfjs.disabled", true);.

Apart from that your code works perfect for me. (Note I used Directory.GetCurrentDirectory() rather than your Setup.DownloadContractPath). Try it with sample.pdf and check if the preferences are actually in about:config page of your webdriver opened Firefox.

Here is a more detailed article: Download PDF files automatically in Firefox using Selenium WebDriver

FirefoxProfile specialProfile = new FirefoxProfile();
specialProfile.SetPreference("browser.helperApps.alwaysAsk.force", false);
specialProfile.SetPreference("browser.download.manager.showWhenStarting", false);
specialProfile.SetPreference("browser.download.folderList", 2);
specialProfile.SetPreference("browser.download.dir", Directory.GetCurrentDirectory()); // my downloading dir
specialProfile.SetPreference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", false);
specialProfile.SetPreference("browser.download.useDownloadDir", true);
specialProfile.SetPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");

specialProfile.SetPreference("pdfjs.disabled", true); // for my Firefox 20.0
return new FirefoxDriver(specialProfile);
Yi Zeng
  • 32,020
  • 13
  • 97
  • 125
  • Glad to know that. So do you know the reason why your code didn't work? – Yi Zeng Apr 12 '13 at 10:13
  • Hi, the missing part was: specialProfile.SetPreference("pdfjs.disabled", true); But i still dont know what this part is doing? And idea? – ravenik Apr 23 '13 at 06:51
  • Firefox has buildin pdf parsing library to view pdf files and it takes control without those lines – Purus May 25 '14 at 06:34
  • It works, until you get added to your mimeTypes.rdf file inside profile root. So if it stops working - delete those lines (or better the whole file, since you can set this all up with browser.helperApps.neverAsk.saveToDisk) – Kudin Jul 27 '17 at 16:02
0

You should be passing options now :

Dim fxOpt As New FirefoxOptions()

fxOpt.Profile.SetPreference("browser.helperApps.alwaysAsk.force", False)
fxOpt.Profile.SetPreference("browser.download.manager.showWhenStarting", False)
fxOpt.Profile.SetPreference("browser.download.folderList", 2)
fxOpt.Profile.SetPreference("browser.download.dir", Directory.GetCurrentDirectory()); // my downloading dir
fxOpt.Profile.SetPreference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", False)
fxOpt.Profile.SetPreference("browser.download.useDownloadDir", True)
fxOpt.Profile.SetPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf")
fxOpt.Profile.SetPreference("pdfjs.disabled", True)

WebDrv = New Firefox.FirefoxDriver(fxOpt)
Jerome
  • 1,153
  • 1
  • 17
  • 28