0

I have tried to set all possible preferences to avoid open and save file dialog while download files using selenium.

It is working for text files but not for PDF files

Below are the preferences set:

    String downloadPath = <some random path>;
    String mimetypes = "application/vnd.pdf,application/vnd.adobe.xfdf,text/csv,application/x-msexcel,application/excel,application/pdf,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml";
    String url = "http://only-testing-blog.blogspot.in/2014/05/login.html";
    FirefoxProfile profile = new FirefoxProfile();
    profile.setPreference("browser.download.panel.shown", false);
    profile.setPreference("browser.download.folderList", 2);
    profile.setPreference("browser.download.manager.showWhenStarting", false);
    profile.setPreference("browser.download.dir", downloadPath);
    profile.setPreference("browser.helperApps.neverAsk.openFile", mimetypes);
    profile.setPreference("browser.helperApps.neverAsk.saveToDisk", mimetypes);
    profile.setPreference("browser.helperApps.alwaysAsk.force", false);
    profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
    profile.setPreference("browser.download.manager.focusWhenStarting", false);
    profile.setPreference("browser.download.manager.useWindow", false);
    profile.setPreference("browser.download.manager.showAlertOnComplete", false);
    profile.setPreference("browser.download.manager.closeWhenDone", false);

    FirefoxDriver driver = new FirefoxDriver(profile);
    driver.get(url);
    driver.findElement(By.xpath("//a[contains(.,'Download PDF File')]")).click();

Am using Firefox 46 with 2.53.0 selenium version

Please help me to make this to work for PDF, Excel and Word files as well

Thanks!

Abhishek kumar
  • 4,347
  • 8
  • 29
  • 44
rama krishna
  • 13
  • 1
  • 5

1 Answers1

0

One solution I found for this is to use the ProfileIni class. It allows you to load a premade firefox profile. You use as shown below

private ProfilesIni init = new ProfilesIni();
private FirefoxProfile profile = init.getProfile("QA");
private WebDriver driver = new WebDriver(profile);

Then, you can set all of those preferences straight on the profile, and set the setting to skip the download dialogue and automatically save to the destination folder you want and keep the settings for every test case you need to download stuff on

kkrenzke
  • 60
  • 3
  • 8