Chromium/Chrome headless - file download not working?

后端 未结 7 1191
遥遥无期
遥遥无期 2020-12-03 05:21

I downloaded the latest version of chromium, to test out the headless feature.

When I run (as root, because I\'m still testing things):

./chrome --no         


        
7条回答
  •  遥遥无期
    2020-12-03 05:57

    I tried today in IdeaJ editor, Java and Maven on Windows 10, and it's working fine:

                    ChromeOptions ds = getDesiredCapabilities(browserName);
    
                    ChromeDriverService driverService = ChromeDriverService.createDefaultService();
                    ChromeDriver driver = new ChromeDriver(driverService, ds);
    
                    Map commandParams = new HashMap<>();
                    commandParams.put("cmd", "Page.setDownloadBehavior");
    
                    Map params = new HashMap<>();
                    params.put("behavior", "allow");
                    params.put("downloadPath", System.getProperty("user.home") + File.separator + "Downloads");
                    commandParams.put("params", params);
    
                    ObjectMapper objectMapper = new ObjectMapper();
                    HttpClient httpClient = HttpClientBuilder.create().build();
    
                    String command = objectMapper.writeValueAsString(commandParams);
    
                    String u = driverService.getUrl().toString() + "/session/" + driver.getSessionId() + "/chromium/send_command";
    
                    HttpPost request = new HttpPost(u);
                    request.addHeader("content-type", "application/json");
                    request.setEntity(new StringEntity(command));
                    httpClient.execute(request);
    

    User this configured "driver" instance to navigate to download file either by URL or by Json/AngularJs download action. Also it sometimes behaved to corrupt file while downloading due to internet slowness.

    More on this, same configuration will work for both Google Chrome UI or Headless execution in latest chrome driver 77~

提交回复
热议问题