Chromium/Chrome headless - file download not working?

后端 未结 7 1184
遥遥无期
遥遥无期 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 06:04

    In Java use following code :

    System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
             ChromeOptions options = new ChromeOptions();
                    options.addArguments("--test-type");
                    options.addArguments("--headless");
                    options.addArguments("--disable-extensions"); //to disable browser extension popup
    
                    ChromeDriverService driverService = ChromeDriverService.createDefaultService();
                    ChromeDriver driver = new ChromeDriver(driverService, options);
    
                    Map commandParams = new HashMap<>();
                    commandParams.put("cmd", "Page.setDownloadBehavior");
                    Map params = new HashMap<>();
                    params.put("behavior", "allow");
                    params.put("downloadPath", "//home//vaibhav//Desktop");
                    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);
            driver.get("http://www.seleniumhq.org/download/");
            driver.findElement(By.linkText("32 bit Windows IE")).click();
    

提交回复
热议问题