简体   繁体   中英

Profile preference for auto download any files in firefox (different browsers also)

public static void main(String[] args)
{
    try
    {
        WebDriver driver = new FirefoxDriver(getFProfile());
        driver.get("http://www.energy.umich.edu/sites/default/files/pdf-sample.pdf");

        WebDriver driver1 = new FirefoxDriver(getFProfile());
        driver1.get("http://samplecsvs.s3.amazonaws.com/SalesJan2009.csv");
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

public static FirefoxProfile getFProfile()
{
    FirefoxProfile firefoxProfile = new FirefoxProfile();

    firefoxProfile.setPreference("browser.download.folderList", 2);
    firefoxProfile.setPreference("browser.download.manager.showWhenStarting", false);
    firefoxProfile.setPreference("browser.download.dir", "${user.home}\\Downloads"); //C:\\download

    //For PDF
    firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");

    //For CSV
    firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "text/csv");

    firefoxProfile.setPreference("pdfjs.disabled", true);

    firefoxProfile.setPreference("plugin.scan.Acrobat", "99.0");
    firefoxProfile.setPreference("plugin.scan.plid.all", false);

    return firefoxProfile;
}

Above code is working for .pdf files only but for .csv prompt was displayed. How can we auto save both using single profile preference setting.

please help me on this.

Use the below modified code for single profile:

 firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf, text/csv");

hope this helps !!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM