Change default download location on Edge chromium

无人久伴 提交于 2020-12-26 12:13:03

问题


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

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

Thanks in Advance


回答1:


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



来源:https://stackoverflow.com/questions/60739613/change-default-download-location-on-edge-chromium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!