我想问问是否有人试图使用Selenium 3.X更改Microsoft Edge Chromium驱动程序上的默认下载位置。
在Chrome浏览器上,我们可以使用类似这样的内容
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("download.default_directory", savePAth);
chromePrefs.put("prompt_for_download", false);
options.setExperimentalOption("prefs", chromePrefs);
信息:
Microsoft Edge浏览器版本:80.0.361.66(官方版本)(64位)
提前致谢
最佳答案
尝试使用以下设置(Java绑定):
public WebDriver newDriver() {
try {
EnvironmentVariables vars = SystemEnvironmentVariables.createEnvironmentVariables();
String version = vars.getProperty("webdriver.edgedriver.version");
WebDriverManager.edgedriver().version(version).setup();
EdgeOptions options = new EdgeOptions();
EdgeDriverService edgeDriverService = EdgeDriverService.createDefaultService();
EdgeDriver edgeDriver = new EdgeDriver(edgeDriverService, options);
final String downloadPath = ${your path}
//************* Enable downloading files / set path *******************
Map<String, Object> commandParams = new HashMap<>();
commandParams.put("cmd", "Page.setDownloadBehavior");
Map<String, String> params = new HashMap<>();
params.put("behavior", "allow");
params.put("downloadPath", downloadPath);
commandParams.put("params", params);
ObjectMapper objectMapper = new ObjectMapper();
HttpClient httpClient = HttpClientBuilder.create().build();
String command = objectMapper.writeValueAsString(commandParams);
String u = edgeDriverService.getUrl().toString() + "/session/" + edgeDriver.getSessionId() + "/chromium/send_command";
HttpPost request = new HttpPost(u);
request.addHeader("content-type", "application/json");
request.setEntity(new StringEntity(command));
httpClient.execute(request);
return edgeDriver;
} catch (Exception e) {
throw new Error(e);
}
}
我可以使用此代码片段将文件下载到所需的路径。来源here
关于selenium-webdriver - 更改Edge Chrome的默认下载位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61142172/