简体   繁体   中英

How to download CSV file through Firefox Profile in Java

Question - After Setting Firefox profile, Firefox keeps asking download location when downloading CSV file. Please help.

System.setProperty("webdriver.gecko.driver", "..\\C_Automation\\Files\\geckodriver64_15.0.exe");
        FirefoxProfile Profile = new FirefoxProfile();
        Profile.setPreference("browser.download.dir",downloadFilepath);
        Profile.setPreference("browser.download.folderList",2);
        Profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "text/csv");
        Profile.setPreference("browser.download.manager.showWhenStarting",false);
        Profile.setPreference("browser.helperApps.neverAsk.openFile","text/csv");
        Profile.setPreference("browser.helperApps.alwaysAsk.force", false);
        driver = new FirefoxDriver(Profile);

I don't see any major issue in your code. But while you setPreference for the FirefoxProfile Class do remember to add all the relevant Multipurpose Internet Mail Extensions of CSV type files referred in this site .

Here is your own code block with some simple tweaks added to it:

String downloadFilepath = "C:\\Utility\\OP_Resources\\MozillaFirefoxDownload";
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
FirefoxProfile Profile = new FirefoxProfile();
Profile.setPreference("browser.download.dir",downloadFilepath);
Profile.setPreference("browser.download.folderList",2);
//neverAsk - saveToDisk - CSV        
Profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/csv,application/excel,application/vnd.ms-excel,application/vnd.msexcel,text/anytext,text/comma-separated-values,text/csv,text/plain,text/x-csv,application/x-csv,text/x-comma-separated-values,text/tab-separated-values");
//neverAsk - saveToDisk - ANY       
Profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/xml,text/plain,text/xml,image/jpeg,application/octet-stream");
Profile.setPreference("browser.download.manager.showWhenStarting",false);
//neverAsk - openFile - CSV
Profile.setPreference("browser.helperApps.neverAsk.openFile","application/csv,application/excel,application/vnd.ms-excel,application/vnd.msexcel,text/anytext,text/comma-separated-values,text/csv,text/plain,text/x-csv,application/x-csv,text/x-comma-separated-values,text/tab-separated-values");
//neverAsk - openFile - ANY
Profile.setPreference("browser.helperApps.neverAsk.openFile","application/xml,text/plain,text/xml,image/jpeg,application/octet-stream");
Profile.setPreference("browser.helperApps.alwaysAsk.force", false);

I have verified my code on this site and Microsoft site as well and seems to me works fine.

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