[英]Download file through RemoteWebDriver and chrome in headless mode
Hello i would like to ask is possible to somehow download file ex.(.csv/.pdf) on selenium node through RemoteWebDriver and in headless mode?您好,我想问一下是否可以通过 RemoteWebDriver 在无头模式下以某种方式在 selenium 节点上下载文件 ex.(.csv/.pdf)? this code below work for headless mode but i cant pass WebDriverServices to RemoteWebDriver....
下面的代码适用于无头模式,但我无法将 WebDriverServices 传递给 RemoteWebDriver ....
chromePrefs.put("download.prompt_for_download", false);
chromePrefs.put("download.directory_upgrade", true);
chromePrefs.put("safebrowsing.enabled", false);
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("safebrowsing.disable_download_protection", true);
chromePrefs.put("download.default_directory", downloadFilepath);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
if (platform.startsWith("local")) {
options.addArguments("--start-maximized");
} else {
options.addArguments("--headless", "--disable-gpu", "--window-size=1024,768","--ignore-certificate-errors","--disable-extensions","--no-sandbox","--disable-dev-shm-usage");
}
caps = DesiredCapabilities.chrome();
caps.setCapability(ChromeOptions.CAPABILITY, options);
caps.setPlatform(getPlatform(platform));
caps.setCapability(ACCEPT_SSL_CERTS, true);
caps.setCapability(ACCEPT_INSECURE_CERTS, true);
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.BROWSER, Level.ALL);
caps.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
options.merge(caps);
ChromeDriverService driverService = ChromeDriverService.createDefaultService();
driver = new ChromeDriver(driverService, caps);
System.out.println(downloadFilepath);
Map< String, Object > commandParams = new HashMap<String, Object>();
commandParams.put("cmd", "Page.setDownloadBehavior");
Map < String, Object > params = new HashMap<String, Object>();
params.put("behavior", "allow");
params.put("downloadPath", downloadFilepath);
commandParams.put("params", params);
ObjectMapper objectMapper = new ObjectMapper();
HttpClient httpClient = HttpClientBuilder.create().build();
try {
String command = objectMapper.writeValueAsString(commandParams);
String u = driverService.getUrl().toString() + "/session/" +
driver.getSessionId() + "/chromium/send_command";
System.out.println("U: " + u);
HttpPost request = new HttpPost(u);
request.addHeader("content-type", "application/json");
request.setEntity(new StringEntity(command));
httpClient.execute(request);
} catch (Exception e) { }
Solution:解决方案:
Just make sharefolder on machine where running selenium node or where have access to and set properties for downloadpath on network path只需在运行 selenium 节点的机器上创建共享文件夹,或者在网络路径上可以访问和设置下载路径的属性
String downloadFilepath = "\\localhost\folderfordownload";
chromePrefs.put("download.default_directory", downloadFilepath);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.