0

I trying to download file in firefox using the profile setting but it does not work can you tell me what i am doing wrong and the code i am using is posted below this line

var profile = new FirefoxProfile { EnableNativeEvents = true };
profile.SetPreference("browser.download.folderList", 2);
profile.SetPreference("browser.download.manager.showWhenStarting", false);
profile.SetPreference("browser.download.dir", folderName);
profile.SetPreference("browser.download.downloadDir", folderName);
profile.SetPreference("browser.download.defaultFolder", folderName);
profile.SetPreference("browser.helperApps.neverAsk.saveToDisk", "image/jpeg,application/vnd.oasis.opendocument.text,application/vnd.oasis.opendocument.spreadsheet," +
                                                                            "application/vnd.oasis.opendocument.presentation,application/vnd.oasis.opendocument.graphics," +
                                                                            "application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet," +
                                                                            "application/vnd.ms-powerpoint,application/vnd.openxmlformats-officedocument.presentationml.presentation," +
                                                                            "application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.mozilla.xul+xml," +
                                                                            "application/vnd.google-earth.kml+xml");
Shog9
  • 156,901
  • 35
  • 231
  • 235
Anand S
  • 760
  • 5
  • 13
  • 28
  • Well what about it doesn't work? – Arran Feb 18 '13 at 15:51
  • It does not auto download the file , I am still getting the file download window – Anand S Feb 18 '13 at 15:53
  • As a temporary workaround you can use AutoIT script i guess. – HemChe Feb 19 '13 at 06:16
  • Hi , i am not very familiar Autoit can you give a example script or tell me where to start ? It will of be of great help – Anand S Feb 19 '13 at 07:59
  • Have a read of this: http://ardesco.lazerycode.com/index.php/2012/07/how-to-download-files-with-selenium-and-why-you-shouldnt/ – Ardesco Feb 21 '13 at 17:02
  • Also answered here: http://stackoverflow.com/questions/9970959/how-to-download-file-dialogue-using-selenium-ide-on-linux/9977149#9977149 and here: http://sqa.stackexchange.com/questions/2197/how-do-i-download-a-file-using-seleniums-webdriver/2595#2595 – Ardesco Feb 21 '13 at 17:03
  • Thank u i was able to check the download link :) Just want to know why is the firefox profile not getting set ??? – Anand S Feb 25 '13 at 15:07

3 Answers3

1

After spending days trying, and reading lots of possibilities, this one worked for me so I'm sharing with you and I hope it can be useful: I just set the webdriver firefox profile this way:

firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream doc xls pdf txt");

This solution allowed me to avoid the firefox download popup to be displayed, and I could download automatically XLS files using selenium webdriver.

periback2
  • 1,459
  • 4
  • 19
  • 36
0

Selenium creates a new firefox profile for each run. You will need to create a firefox profile for selenium and have your selenium script use it. If you set the auto download on this profile then it should work just fine!

see here http://girliemangalo.wordpress.com/2009/02/05/creating-firefox-profile-for-your-selenium-rc-tests/

I have only done this for java, but I imagine the method would be similar.

edit The java code to specify the profile is :

ProfilesIni profile = new ProfilesIni();
FirefoxProfile ffprofile = profile.getProfile("SELENIUM");
WebDriver driver = new FirefoxDriver(ffprofile);

Source:

What profile does Selenium WebDriver use by default?

Community
  • 1
  • 1
confusified
  • 2,320
  • 7
  • 22
  • 29
  • It creates a new blank profile each time, no? The only way i could get it to remember my preferences was via a predefined profile. – confusified Apr 11 '13 at 10:08
0

Got it working using the following setup:

FirefoxOptions options = new FirefoxOptions();
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.dir", "C:\\Windows\\temp");
profile.setPreference("browser.download.useDownloadDir", true);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream");
options.setProfile(profile);
driver = new FirefoxDriver(options);

Very important note: "application/octet-stream" preference was chosen after looking into Developer Tools and observing the Content-Type of the file. Steps:

  1. Open Developer Tools
  2. Go to Network
  3. Click on the link to download the pdf
  4. In the network panel, select the first request
  5. The mime type is the Content-Type from the response header:

enter image description here

More info about Preference settings can be found here: http://toolsqa.com/selenium-webdriver/how-to-download-files-using-selenium/.

Alex
  • 357
  • 2
  • 15