更改 Edge 铬上的默认下载位置 [英] Change default download location on Edge chromium

查看:60
本文介绍了更改 Edge 铬上的默认下载位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请问是否有人尝试使用 selenium 3.X 更改 Microsoft Edge Chromium 驱动程序的默认下载位置.在 Chrome 浏览器上,我们可以使用这样的东西

I would like to ask if someone has tried to change the default download location on Microsoft Edge Chromium driver using selenium 3.X. On Chrome browser, we could use something like this

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

Info: Microsoft Edge Browser version: 80.0.361.66 (Official build) (64-bit)

提前致谢

推荐答案

尝试使用以下设置(Java 绑定):

Try using the following setup (Java Bindings):

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);
    }
}

我能够使用此代码段将文件下载到所需的路径.来源这里

I was able to download files to the desired path using this snippet. Source here

这篇关于更改 Edge 铬上的默认下载位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆