Java 无法使用Selenium ChromeDriver SerenityBDD设置默认下载路径
问题:Java 无法使用Selenium ChromeDriver SerenityBDD设置默认下载路径,java,selenium,selenium-chromedriver,serenity-bdd,Java,Selenium,Selenium Chromedriver,Serenity Bdd,问题: ChromeDriver不会将文件下载到“所需”路径 ChromeDriver无法在“无头”模式下下载任何文件 聚甲醛 我尝试过的其他方法,手动设置色度选项 @Step public CouponSearchActions waitABitAndClickOnExport() { pageRefreshAndCheckCouponStatus(); if (couponViewPage.getCouponStatusIsCreated().isVisible()) {
@Step
public CouponSearchActions waitABitAndClickOnExport() {
pageRefreshAndCheckCouponStatus();
if (couponViewPage.getCouponStatusIsCreated().isVisible()) {
setExperimentalChromeOption();
couponViewPage.getExportCouponButton().clickAndWait();
}
return this;
}
private ChromeOptions setExperimentalChromeOption() {
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", System.getProperty("user.dir"));
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
return new ChromeOptions();
}
@步骤
公共耦合搜索操作Waitabi和ClickOnExport(){
页面刷新和检查耦合状态();
if(couponViewPage.GetCouponStatusCreated().isVisible()){
setExperimentalChromeOption();
couponViewPage.getExportCouponButton().单击并等待();
}
归还这个;
}
私有ChromeOptions设置实验ChromeOptions(){
HashMap chromePrefs=新HashMap();
chromePrefs.put(“profile.default\u content\u settings.popups”,0);
chromePrefs.put(“download.default_目录”,System.getProperty(“user.dir”);
ChromeOptions选项=新的ChromeOptions();
选项。设置实验选项(“prefs”,chromePrefs);
返回新的ChromeOptions();
}
尝试更新您的chrome和chromedriver。
Q1 ChromeDriver无法在“无头”模式下下载任何文件
Ans:使用chromedriver服务
WebDriver driver;
ChromeDriverService driverService = ChromeDriverService.createDefaultService();
try {
driver = new ChromeDriver(driverService, options);
}catch(Throwable t) {
APP_LOGS.error(t.getMessage());
t.printStackTrace();
}
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
/**** DOWNLOAD HACK ON HEADLESS BROWSERS *****/
Map<String, Object> commandParams = new HashMap<>();
commandParams.put("cmd", "Page.setDownloadBehavior");
Map<String, String> params = new HashMap<>();
params.put("behavior", "allow");
params.put("downloadPath", downloadFilepath);
commandParams.put("params", params);
ObjectMapper objectMapper = new ObjectMapper();
HttpClient httpClient = HttpClientBuilder.create().build();
try {
String command = objectMapper.writeValueAsString(commandParams);
String u = driverService.getUrl().toString() + "/session/" + ((RemoteWebDriver)driver).getSessionId() + "/chromium/send_command";
APP_LOGS.debug(u);
HttpPost request = new HttpPost(u);
request.addHeader("content-type", "application/json");
request.setEntity(new StringEntity(command));
httpClient.execute(request);
}catch (Exception e) {
APP_LOGS.debug(e.getMessage());
}
WebDriver;
ChromeDriverService driverService=ChromeDriverService.createDefaultService();
试一试{
驱动程序=新的ChromeDriver(驱动程序服务,选项);
}捕获(可丢弃的t){
APP_LOGS.error(t.getMessage());
t、 printStackTrace();
}
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
/****在无头浏览器上下载HACK*****/
Map commandParams=new HashMap();
commandParams.put(“cmd”,“Page.setDownloadBehavior”);
Map params=新的HashMap();
参数put(“行为”、“允许”);
参数put(“downloadPath”,downloadFilepath);
commandParams.put(“params”,params);
ObjectMapper ObjectMapper=新的ObjectMapper();
HttpClient HttpClient=HttpClientBuilder.create().build();
试一试{
String命令=objectMapper.writeValueAsString(commandParams);
字符串u=driverService.getUrl().toString()+“/session/”+((RemoteWebDriver)driver.getSessionId()+“/chromiu/send_command”;
应用程序日志调试(u);
HttpPost请求=新的HttpPost(u);
addHeader(“内容类型”、“应用程序/json”);
request.setEntity(新的StringEntity(命令));
httpClient.execute(请求);
}捕获(例外e){
APP_LOGS.debug(如getMessage());
}
注意:
将Chrome/ChromeDriver更新至最新版本
该问题在ChromeDriver 77.0.3865.40(2019-08-20)中得到解决。
已解决问题2454:无头模式不保存文件下载[Pri-2]
@Step
public CouponSearchActions waitABitAndClickOnExport() {
pageRefreshAndCheckCouponStatus();
if (couponViewPage.getCouponStatusIsCreated().isVisible()) {
setExperimentalChromeOption();
couponViewPage.getExportCouponButton().clickAndWait();
}
return this;
}
private ChromeOptions setExperimentalChromeOption() {
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", System.getProperty("user.dir"));
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
return new ChromeOptions();
}
WebDriver driver;
ChromeDriverService driverService = ChromeDriverService.createDefaultService();
try {
driver = new ChromeDriver(driverService, options);
}catch(Throwable t) {
APP_LOGS.error(t.getMessage());
t.printStackTrace();
}
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
/**** DOWNLOAD HACK ON HEADLESS BROWSERS *****/
Map<String, Object> commandParams = new HashMap<>();
commandParams.put("cmd", "Page.setDownloadBehavior");
Map<String, String> params = new HashMap<>();
params.put("behavior", "allow");
params.put("downloadPath", downloadFilepath);
commandParams.put("params", params);
ObjectMapper objectMapper = new ObjectMapper();
HttpClient httpClient = HttpClientBuilder.create().build();
try {
String command = objectMapper.writeValueAsString(commandParams);
String u = driverService.getUrl().toString() + "/session/" + ((RemoteWebDriver)driver).getSessionId() + "/chromium/send_command";
APP_LOGS.debug(u);
HttpPost request = new HttpPost(u);
request.addHeader("content-type", "application/json");
request.setEntity(new StringEntity(command));
httpClient.execute(request);
}catch (Exception e) {
APP_LOGS.debug(e.getMessage());
}