更改 Edge chromium 上的默认下载位置

2024-05-07

我想问是否有人尝试使用 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 https://bugs.chromium.org/p/chromium/issues/detail?id=696481#c78

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

更改 Edge chromium 上的默认下载位置 的相关文章

随机推荐