从Selenium和chromedriver下载文件

我无法让Selenium和Chrome(Canary)下载文件。 我正在使用Java和Chrome 59/60(因为我的测试适用于Windows和Linux),我正在尝试从网页开始下载文件。

当我从selenium不设置无头模式时,会打开chrome窗口并下载文件。

当我设置--headless标志时,chrome窗口不会打开,并且下载不会启动。

  public static void chromeDownload() throws IOException, InterruptedException{ ChromeOptions options = new ChromeOptions(); String downloadFilepath = ""; if (ValidateOS.isWindows()){ System.out.println("This is a Windows system."); System.setProperty("webdriver.chrome.driver", "resources\\driver\\chromedriver.exe"); options.setBinary("C:\\Users\\Juri\\AppData\\Local\\Google\\Chrome SxS\\Application\\chrome.exe"); downloadFilepath = "C:\\"; } else if (ValidateOS.isUnix()){ System.out.println("This is a Unix system."); System.setProperty("webdriver.chrome.driver", "resources/driver/chromedriver"); options.setBinary("/usr/bin/google-chrome"); downloadFilepath = "/home/juri/"; } // Manage the download HashMap chromePrefs = new HashMap(); chromePrefs.put("profile.default_content_settings.popups", 0); chromePrefs.put("download.default_directory", downloadFilepath); // Save Chrome Options HashMap chromeOptionsMap = new HashMap(); options.setExperimentalOption("prefs", chromePrefs); options.addArguments("--headless --disable-gpu"); DesiredCapabilities cap = DesiredCapabilities.chrome(); cap.setCapability(ChromeOptions.CAPABILITY, chromeOptionsMap); cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true); cap.setCapability(ChromeOptions.CAPABILITY, options); ChromeDriver driver = new ChromeDriver(cap); driver.get("http://localhost/my-test-page.html"); driver.findElement(By.id("download")).click(); Thread.sleep(5000); // wait 5 seconds for a small file to download.. yes.. I know... driver.quit(); } 

在Click ,在GUI模式下,下载开始。 在无头模式下,它没有。

怎么解决?

OT

我正在使用Chrome Canary,它的v.60具有 – 无function。 在没有gui的服务器上运行抓取器非常方便。 但是,出于同样的原因……我发现在没有GUI的服务器上下载Chrome是没用的。 除了主要问题..我想知道你是否开发人员认为在Linux服务器上安装chrome只是为了在无头模式下启动它是可以的。

更新 :我仍在寻找一个解决方案,如果有人会读到这个:/搜索结果有几个,我尝试了所有

通过调整此链接中的代码解决: 使用ChromeDriver和无头模式下载Java,Selenium中的文件

对于那些想知道我的代码现在如何……

 public static void chromeDownload(String address, String Headless, String DownDir) throws IOException, InterruptedException{ ChromeOptions options = new ChromeOptions(); String downloadFilepath = DownDir; if (ValidateOS.isWindows()){ System.out.println("This is a Windows system."); System.setProperty("webdriver.chrome.driver", "resources\\driver\\chromedriver.exe"); //options.setBinary("C:\\Users\\Juri\\AppData\\Local\\Google\\Chrome SxS\\Application\\chrome.exe"); // If this is commented, the grabber will use the main Chrome } else if (ValidateOS.isUnix()){ System.out.println("This is a Unix system."); System.setProperty("webdriver.chrome.driver", "resources/driver/chromedriver"); options.setBinary("/usr/bin/google-chrome"); } switch (Headless.toUpperCase()){ case "TRUE": options.addArguments("--headless --disable-gpu"); break; case "FALSE": default: options.addArguments("--window-size=1152,768"); break; } options.addArguments("--test-type"); options.addArguments("--disable-extension"); 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", downloadFilepath); params.put("cmd", "Page.setDownloadBehavior"); commandParams.put("params", params); ObjectMapper objectMapper = new ObjectMapper(); CloseableHttpClient 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(address); driver.findElement(By.id("download")).click(); driver.quit(); }