Downloading the files in headless chrome

Hi

I want to download the file in headless chrome.
I’m Using the below code:

HashMap<Object, String> chromePrefs = new HashMap<Object, String>()
chromePrefs.put(“download.default_directory”, RunConfiguration.getProjectDir() + “/Include/TestData/DownloadedFiles”)
chromePrefs.put(“browser.setDownloadBehavior”, “allow”)
RunConfiguration.setWebDriverPreferencesProperty(“prefs”, chromePrefs)

But it’s not working,Is there anyway to accomplish this?

hello,

sorry this not work, need to investigate why
String downloadPath = "C:\\Users\\xxxx\\Desktop\\data\\pdf\\"
Map<String, Object> chromePrefs = new HashMap<String, Object>()
chromePrefs.put("download.default_directory", downloadPath)
chromePrefs.put("download.prompt_for_download", false)
chromePrefs.put("pdfjs.disabled", true);
System.setProperty("webdriver.chrome.driver", DriverFactory.getChromeDriverPath())
ChromeOptions options = new ChromeOptions()
options.addArguments("--headless")
options.setExperimentalOption("prefs", chromePrefs)
WebDriver driver = new ChromeDriver(options)
driver.get("https://docs.oracle.com/javaee/7/JEETT.pdf");
WebUI.delay(10)
System.out.println("Task complete, please go to save folder to see it.");
driver.close()

got it to work in Jidea, now i try to move it into Katalon

ok,

this will work

import org.apache.hc.client5.http.classic.HttpClient
import org.apache.hc.client5.http.classic.methods.HttpPost
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder
import org.apache.hc.core5.http.io.entity.StringEntity
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.chrome.ChromeDriverService
import org.openqa.selenium.chrome.ChromeOptions
import org.openqa.selenium.remote.CapabilityType
import org.openqa.selenium.remote.DesiredCapabilities

import com.fasterxml.jackson.databind.ObjectMapper
import com.kms.katalon.core.webui.driver.DriverFactory
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

String downloadFilepath = "C:\\Users\\xxxx\\Desktop\\data\\pdf\\"
HashMap<String, Object> chromePreferences = new HashMap<String, Object>();
chromePreferences.put("profile.default_content_settings.popups", 0);
chromePreferences.put("download.prompt_for_download", "false");
chromePreferences.put("download.default_directory", downloadFilepath);
ChromeOptions chromeOptions = new ChromeOptions();
System.setProperty("webdriver.chrome.driver", DriverFactory.getChromeDriverPath())

chromeOptions.addArguments("start-maximized");
chromeOptions.addArguments("disable-infobars");

//HEADLESS CHROME
chromeOptions.addArguments("headless");

chromeOptions.setExperimentalOption("prefs", chromePreferences);
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, chromeOptions);

ChromeDriverService driverService = ChromeDriverService.createDefaultService();
ChromeDriver driver = new ChromeDriver(driverService, chromeOptions);

Map<String, Object> commandParams = new HashMap<>();
commandParams.put("cmd", "Page.setDownloadBehavior");
Map<String, String> params = new HashMap<>();
params.put("behavior", "allow");
params.put("downloadPath", downloadFilepath);
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));
try {
	httpClient.execute(request);
	driver.get("https://docs.oracle.com/javaee/7/JEETT.pdf");
	WebUI.delay(30)
	System.out.println("Task complete, please go to save folder to see it.");
	driver.close()
} catch (IOException e2) {
	e2.printStackTrace();
}

HI Matt

verified, works fine in my lap top

import org.apache.hc.client5.http.classic.HttpClient
import org.apache.hc.client5.http.classic.methods.HttpPost
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder
import org.apache.hc.core5.http.io.entity.StringEntity
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.chrome.ChromeDriverService
import org.openqa.selenium.chrome.ChromeOptions
import org.openqa.selenium.remote.CapabilityType
import org.openqa.selenium.remote.DesiredCapabilities

import com.fasterxml.jackson.databind.ObjectMapper
import com.kms.katalon.core.webui.driver.DriverFactory
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

String downloadFilepath = “C:\Users\fitim\Desktop\data\pdf\”
HashMap<String, Object> chromePreferences = new HashMap<String, Object>();
chromePreferences.put(“profile.default_content_settings.popups”, 0);
chromePreferences.put(“download.prompt_for_download”, “false”);
chromePreferences.put(“download.default_directory”, downloadFilepath);
ChromeOptions chromeOptions = new ChromeOptions();
System.setProperty(“webdriver.chrome.driver”, DriverFactory.getChromeDriverPath())

chromeOptions.addArguments(“start-maximized”);
chromeOptions.addArguments(“disable-infobars”);

//HEADLESS CHROME
chromeOptions.addArguments(“headless”);

chromeOptions.setExperimentalOption(“prefs”, chromePreferences);
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, chromeOptions);

ChromeDriverService driverService = ChromeDriverService.createDefaultService();
ChromeDriver driver = new ChromeDriver(driverService, chromeOptions);

Map<String, Object> commandParams = new HashMap<>();
commandParams.put(“cmd”, “Page.setDownloadBehavior”);
Map<String, String> params = new HashMap<>();
params.put(“behavior”, “allow”);
params.put(“downloadPath”, downloadFilepath);
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));
try {
httpClient.execute(request);
driver.get(“https://docs.oracle.com/javaee/7/JEETT.pdf”);
WebUI.delay(30)
System.out.println(“Task complete, please go to save folder to see it.”);
driver.close()
} catch (IOException e2) {
e2.printStackTrace();
}

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Users/fitim/KatalonStudio/Katalon_Studio_Windows_64-7.0.10/plugins/ch.qos.logback.classic_1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/fitim/KatalonFromGitHub/KatalonProject/Drivers/imageTxt.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/fitim/KatalonStudio/Katalon_Studio_Windows_64-7.0.10/configuration/plugin/install/31074/1.0.0/katalon-studio-jmeter-integration-plugin-1.0.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
2019-12-06 13:18:37.040 INFO c.k.katalon.core.main.TestCaseExecutor - --------------------
2019-12-06 13:18:37.175 INFO c.k.katalon.core.main.TestCaseExecutor - START Test Cases/ChromeOptions/ChromeHeadlessDownload
Test Cases/ChromeOptions/ChromeHeadlessDownload
[:]
testCaseContext.getTextCaseId()=Test Cases/ChromeOptions/ChromeHeadlessDownload
2019-12-06 13:18:56.619 DEBUG testcase.ChromeHeadlessDownload - 1: downloadFilepath = “C:\Users\fitim\Desktop\data\pdf”
2019-12-06 13:18:56.656 DEBUG testcase.ChromeHeadlessDownload - 2: chromePreferences = new java.util.HashMap()
2019-12-06 13:18:56.853 DEBUG testcase.ChromeHeadlessDownload - 3: chromePreferences.put(“profile.default_content_settings.popups”, 0)
2019-12-06 13:18:56.861 DEBUG testcase.ChromeHeadlessDownload - 4: chromePreferences.put(“download.prompt_for_download”, “false”)
2019-12-06 13:18:56.912 DEBUG testcase.ChromeHeadlessDownload - 5: chromePreferences.put(“download.default_directory”, downloadFilepath)
2019-12-06 13:18:56.916 DEBUG testcase.ChromeHeadlessDownload - 6: chromeOptions = new org.openqa.selenium.chrome.ChromeOptions()
2019-12-06 13:18:57.736 DEBUG testcase.ChromeHeadlessDownload - 7: System.setProperty(“webdriver.chrome.driver”, getChromeDriverPath())
2019-12-06 13:18:58.325 DEBUG testcase.ChromeHeadlessDownload - 8: chromeOptions.addArguments(“start-maximized”)
2019-12-06 13:18:58.523 DEBUG testcase.ChromeHeadlessDownload - 9: chromeOptions.addArguments(“disable-infobars”)
2019-12-06 13:18:58.679 DEBUG testcase.ChromeHeadlessDownload - 10: chromeOptions.addArguments(“headless”)
2019-12-06 13:18:58.904 DEBUG testcase.ChromeHeadlessDownload - 11: chromeOptions.setExperimentalOption(“prefs”, chromePreferences)
2019-12-06 13:18:59.128 DEBUG testcase.ChromeHeadlessDownload - 12: cap = DesiredCapabilities.chrome()
joulukuuta 06, 2019 1:18:59 IP. org.openqa.selenium.remote.DesiredCapabilities chrome
INFO: Using new ChromeOptions() is preferred to DesiredCapabilities.chrome()
2019-12-06 13:18:59.328 DEBUG testcase.ChromeHeadlessDownload - 13: cap.setCapability(ACCEPT_SSL_CERTS, true)
2019-12-06 13:18:59.444 DEBUG testcase.ChromeHeadlessDownload - 14: cap.setCapability(CAPABILITY, chromeOptions)
2019-12-06 13:18:59.831 DEBUG testcase.ChromeHeadlessDownload - 15: driverService = ChromeDriverService.createDefaultService()
2019-12-06 13:19:00.981 DEBUG testcase.ChromeHeadlessDownload - 16: driver = new org.openqa.selenium.chrome.ChromeDriver(driverService, chromeOptions)
Starting ChromeDriver 77.0.3865.40 (f484704e052e0b556f8030b65b953dce96503217-refs/branch-heads/3865@{#442}) on port 32301
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
[1575631156.663][WARNING]: This version of ChromeDriver has not been tested with Chrome version 78.
[1575631156.727][WARNING]: This version of ChromeDriver has not been tested with Chrome version 78.
[1575631157.828][WARNING]: This version of ChromeDriver has not been tested with Chrome version 78.
[1575631158.934][WARNING]: This version of ChromeDriver has not been tested with Chrome version 78.
[1575631159.999][WARNING]: This version of ChromeDriver has not been tested with Chrome version 78.
[1575631162.633][WARNING]: Timed out connecting to Chrome, retrying…
joulukuuta 06, 2019 1:19:25 IP. org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
2019-12-06 13:19:25.472 DEBUG testcase.ChromeHeadlessDownload - 17: commandParams = new java.util.HashMap()
2019-12-06 13:19:25.518 DEBUG testcase.ChromeHeadlessDownload - 18: commandParams.put(“cmd”, “Page.setDownloadBehavior”)
2019-12-06 13:19:25.595 DEBUG testcase.ChromeHeadlessDownload - 19: params = new java.util.HashMap()
2019-12-06 13:19:25.675 DEBUG testcase.ChromeHeadlessDownload - 20: params.put(“behavior”, “allow”)
2019-12-06 13:19:25.690 DEBUG testcase.ChromeHeadlessDownload - 21: params.put(“downloadPath”, downloadFilepath)
2019-12-06 13:19:25.702 DEBUG testcase.ChromeHeadlessDownload - 22: commandParams.put(“params”, params)
2019-12-06 13:19:25.995 DEBUG testcase.ChromeHeadlessDownload - 23: objectMapper = new com.fasterxml.jackson.databind.ObjectMapper()
2019-12-06 13:19:29.629 DEBUG testcase.ChromeHeadlessDownload - 24: httpClient = create().build()
2019-12-06 13:19:31.067 DEBUG testcase.ChromeHeadlessDownload - 25: command = objectMapper.writeValueAsString(commandParams)
2019-12-06 13:19:31.328 DEBUG testcase.ChromeHeadlessDownload - 26: u = getUrl().toString() + “/session/” + driver.getSessionId() + “/chromium/send_command”
2019-12-06 13:19:31.515 DEBUG testcase.ChromeHeadlessDownload - 27: request = new org.apache.hc.client5.http.classic.methods.HttpPost(u)
2019-12-06 13:19:32.512 DEBUG testcase.ChromeHeadlessDownload - 28: request.addHeader(“content-type”, “application/json”)
2019-12-06 13:19:32.652 DEBUG testcase.ChromeHeadlessDownload - 29: request.setEntity(new org.apache.hc.core5.http.io.entity.StringEntity(command))
2019-12-06 13:19:33.832 DEBUG testcase.ChromeHeadlessDownload - 30: try
2019-12-06 13:19:33.840 DEBUG testcase.ChromeHeadlessDownload - 1: httpClient.execute(request)
2019-12-06 13:19:34.794 DEBUG testcase.ChromeHeadlessDownload - 2: driver.get(“https://docs.oracle.com/javaee/7/JEETT.pdf”)
2019-12-06 13:19:38.694 DEBUG testcase.ChromeHeadlessDownload - 3: delay(30)
2019-12-06 13:20:09.305 DEBUG testcase.ChromeHeadlessDownload - 4: out.println(“Task complete, please go to save folder to see it.”)
Task complete, please go to save folder to see it.
2019-12-06 13:20:09.534 DEBUG testcase.ChromeHeadlessDownload - 5: driver.close()
2019-12-06 13:20:10.439 INFO c.k.katalon.core.main.TestCaseExecutor - END Test Cases/ChromeOptions/ChromeHeadlessDownload

what kind of issues you have?

Thank you for checking! Luckily I was able to resolve this due to an issue with escaped characters in my file path. It was working, just not saving to the location I expected.

:white_check_mark: Confirmed that your driver config works with headless file downloads.