问题 Chromium / Chrome无头 - 文件下载不起作用?


我下载了最新版本的铬,以测试无头功能。

当我跑(作为根,因为我还在测试东西):

./chrome --no-sandbox http://cp7.awardspace.com/speed-test/awardspace-data1mb.zip

在GUI终端中,它打开Chromium并下载文件。

如果我试图无头地运行它,我输入以下内容:

./chrome --no-sandbox --headless http://cp7.awardspace.com/speed-test/awardspace-data1mb.zip

终端输出一些信息,没有窗口打开,但是:我没有在任何地方下载文件。

我一直在寻找互联网和讨论组以获取更多信息,但找不到任何东西。

文件下载是否在Chromium的无头模式下工作?


12046
2018-02-26 16:35


起源

[ 这个](bugs.chromium.org/p/chromium/issues/detail?id=696481#c80)解决方案对我来说很完美(Python) - applekate


答案:


这是无头实施中报告的错误: https://bugs.chromium.org/p/chromium/issues/detail?id=696481


9
2018-03-03 15:54





在Java中使用以下代码:

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<String, Object> commandParams = new HashMap<>();
                commandParams.put("cmd", "Page.setDownloadBehavior");
                Map<String, String> 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();

2
2018-03-28 14:05



你可能想对这段代码给出一个详细的概述,给出一个没有任何解释的大块代码将无法帮助OP - Verv


注意:不完全回答问题,但解决了问题

我研究了很多关于使用不同的参数/选项/偏好进行无头镀铬下载,但没有任何效果。然后我使用Apache Commons的FileUtils使用标准的Java方式下载文件

FileUtils.copyURLToFile(URI, FILE);

0
2017-10-21 16:39





借助Chrome Remote Interface,我能够使用chrome headless下载文件

public void TryEnableFileDownloading(string downloadPath)
{
    TrySendCommand("Page.setDownloadBehavior", new Dictionary<string, object>()
    {
        ["behavior"] = "allow",
        ["downloadPath"] = downloadPath
    });
}

可以在此处找到与selenium集成的完整代码 https://github.com/cezarypiatek/Tellurium/blob/master/Src/MvcPages/SeleniumUtils/ChromeRemoteInterface/ChromeRemoteInterface.cs

有关setDownloadBehavior和Chrome Remote界面的更多信息 https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-setDownloadBehavior


0
2017-11-10 23:50



你可以发布这个工作的java代码吗? - Coded9
@ Coded9过去8年我没有在java中编写任何代码,所以可能需要花费大量时间来重新设置环境。这里的关键是访问CommandExecutor,它是WebDriver的私有字段。我不知道在反射方面有什么可能性给你java。如果您能够访问私有成员,您应该能够轻松地自己实现此功能。有关详细信息,请检查我的repo中WebDriverCommandExecuter类的实现,该类允许我通过WebDriver使用ChromeRemoteInterface - cezarypiatek