一尘不染

如何在Java中通过Firefox配置文件下载CSV文件

selenium

问题-设置Firefox配置文件后,Firefox在下载CSV文件时一直询问下载位置。请帮忙。

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);

阅读 257

收藏
2020-06-26

共1个答案

一尘不染

我看不到您的代码中的任何重大问题。但是,当你setPreferenceFirefoxProfile等级不记得添加所有相关Multipurpose Internet Mail Extensions的CSV类型的文件在此提到的网站

这是您自己的代码块,其中添加了一些简单的调整:

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);

我已经在此站点和Microsoft
网站上验证了我的代码,并且在我看来效果很好。

2020-06-26