我关注@BeforeClass:
@BeforeClass
public static void setUpClass() {
FirefoxOptions options = new FirefoxOptions();
options.setBinary("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
System.setProperty("webdriver.gecko.driver", "C:\\Users\\pburgr\\Desktop\\geckodriver-v0.19.1-win64\\geckodriver.exe");
driver = new FirefoxDriver(options);
driver.manage().window().maximize();
FirefoxProfile FF_profile = new FirefoxProfile();
FF_profile.setPreference("browser.download.folderList",2);
FF_profile.setPreference("browser.download.manager.showWhenStarting",false);
FF_profile.setPreference("browser.download.dir","C:\\users\\pburgr\\downloads\\");
FF_profile.setPreference("browser.helperApps.alwaysAsk.force", false);
FF_profile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
FF_profile.setPreference("pdfjs.disabled", true);
}
但是在下载xlsx文件时,我仍然停留在下载对话框中。我错过了什么? THX
答案 0 :(得分:0)
我将options.setProfile(selenium_profile);
移到了setPreferences命令后面,它可以正常工作
另一种方法是在firefox.exe -p中创建manualy新的浏览器配置文件。自定义配置文件(自动下载xls文件)并以这种方式启动selenium:
@BeforeClass
public static void setUpClass() {
FirefoxOptions options = new FirefoxOptions();
ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile selenium_profile = allProfiles.getProfile("selenium_profile");
options.setProfile(selenium_profile);
options.setBinary("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
System.setProperty("webdriver.gecko.driver", "C:\\Users\\pburgr\\Desktop\\geckodriver-v0.20.0-win64\\geckodriver.exe");
driver = new FirefoxDriver(options);
driver.manage().window().maximize();}