Issue
Hello i would like to ask is possible to somehow download file ex.(.csv/.pdf) on selenium node through RemoteWebDriver and in headless mode? this code below work for headless mode but i cant pass WebDriverServices to 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
Solution:
Just make sharefolder on machine where running selenium node or where have access to and set properties for downloadpath on network path
String downloadFilepath = "\\localhost\folderfordownload";
chromePrefs.put("download.default_directory", downloadFilepath);
Answered By - gtmaster01
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.