selenium-webdriver - 更改Edge Chrome的默认下载位置

标签 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位)

提前致谢

最佳答案

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

相关文章:

java - 如何将 logger.error 报告的错误转储到文件中

css - 渐变动画使 CSS 无法在 Internet Explorer/Edge/Safari 上正确显示

javascript - 在 Internet Explorer 中添加对隐藏元素的选择会引发错误

javascript - getComputedStyle() 跨浏览器的一致性

python - 在 Selenium 中更改 Google Chrome 用户代理的方法?

python - 使用 Python 和 WebDriver 断言/验证ElementPresent?

python-3.x - Selenium webdriver 导入问题

css - 谷歌字体未在 IE 或边缘加载

css - 删除 Stack Exchange 链接的下划线样式而不闪烁

python - 如何关闭 Selenium 中的打印对话框?