更改Edge Chrome的默认下载位置

时间:2020-03-18 12:29:14

标签: selenium-webdriver microsoft-edge

我想问问是否有人试图使用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位)

预先感谢

1 个答案:

答案 0 :(得分:1)

尝试使用以下设置(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

相关问题