通过 RemoteWebDriver 和无头模式 chrome 下载文件
Download file through RemoteWebDriver and chrome in headless mode
您好,我想问一下是否可以通过 RemoteWebDriver 以无头模式在 selenium 节点上以某种方式下载文件 ex.(.csv/.pdf)?下面的代码适用于无头模式,但我无法将 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) { }
解法:
只需在 运行 selenium 节点或有权访问并设置网络路径下载路径属性的计算机上创建共享文件夹
String downloadFilepath = "\localhost\folderfordownload";
chromePrefs.put("download.default_directory", downloadFilepath);
您好,我想问一下是否可以通过 RemoteWebDriver 以无头模式在 selenium 节点上以某种方式下载文件 ex.(.csv/.pdf)?下面的代码适用于无头模式,但我无法将 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) { }
解法:
只需在 运行 selenium 节点或有权访问并设置网络路径下载路径属性的计算机上创建共享文件夹
String downloadFilepath = "\localhost\folderfordownload";
chromePrefs.put("download.default_directory", downloadFilepath);