由于我还不清楚如何在ChromeDriver中使用--headless模式下载文件 - selenium [Java],请在此处添加示例,如何操作,我尝试这样做(文件下载没有--headless选项正常工作):
ChromeOptions lChromeOptions = new ChromeOptions();
HashMap<String, Object> lChromePrefs = new HashMap<String, Object>();
lChromePrefs.put("profile.default_content_settings.popups", 0);
lChromePrefs.put("download.default_directory", _PATH_TO_DOWNLOAD_DIR);
lChromePrefs.put("browser.set_download_behavior", "{ behavior: 'allow' , downloadPath: '"+_PATH_TO_DOWNLOAD_DIR+"'}");
lChromeOptions.addArguments("--headless");
lChromeOptions.addArguments("--disable-gpu");
lChromeOptions.setExperimentalOption("prefs", lChromePrefs);
WebDriver lWebDriver = new ChromeDriver(lChromeOptions);
据我所知,自从Chrome v60 +设置Browser.setDownloadBehaviour(true, _DIRECTORY)
以来,无法以无头模式下载文件,但我无法找到ChromeDriver是否已经支持它的信息,或者只是我使用错误的chrome首选项作为参数
ChromeDriver版本:2.34 Selenium + WebDriver版本:3.8.1
答案 0 :(得分:6)
在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();
答案 1 :(得分:0)
根据chrome驱动程序的正式发布页面,已针对此问题引入了修复程序。任何高于77的chrome驱动程序版本都可以无头模式下载文件。
options.addArguments("--headless");