Download FIle Chrome headless

Hi Guys,

Any idea how to download a file in chrome headless mode?. Thanks.

Hi @pgonzalez

To download a file in Chrome headless, you can preset via the Desired Capabilities settings. The solution and an example you can find at this post:
Chrome:

Firefox:

Hi , thanks for this, it worked configuring it in settings >> Desired capabilities >> Chrome headless and set the absolute path

But I have another question, is it possible to set up a variable in the value ? instead of writing the path value “c:\user\download etc” , set a variable containing the relative path instead ?

@pgonzalez

You can customize the download folder by modifying the desired capabilities during the execution like this:

import com.kms.katalon.core.configuration.RunConfiguration
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI


Map desiredCapabilities = RunConfiguration.getDriverPreferencesProperties("WebUI")
Map prefs = desiredCapabilities.get("prefs")
prefs.put("download.default_directory", "Your location")
RunConfiguration.setWebDriverPreferencesProperty("prefs", prefs)

WebUI.openBrowser('')

Doesn’t work for me, not downloading anything :frowning:

Here the log:
2020-08-26 11:13:26.683 INFO c.k.katalon.core.main.TestSuiteExecutor - START Test Suites/6.UTILITY MENU
2020-08-26 11:13:26.746 INFO c.k.katalon.core.main.TestSuiteExecutor - hostName = PGonzalez - Pilar-Laptop.PEGASUS.local
2020-08-26 11:13:26.748 INFO c.k.katalon.core.main.TestSuiteExecutor - os = Windows 10 64bit
2020-08-26 11:13:26.749 INFO c.k.katalon.core.main.TestSuiteExecutor - hostAddress = 10.1.1.81
2020-08-26 11:13:26.750 INFO c.k.katalon.core.main.TestSuiteExecutor - katalonVersion = 7.6.5.205
2020-08-26 11:13:27.235 INFO c.k.katalon.core.main.TestCaseExecutor - --------------------
2020-08-26 11:13:27.235 INFO c.k.katalon.core.main.TestCaseExecutor - START Test Cases/6.UTILITY MENU/Data Export Site
2020-08-26 11:13:27.264 INFO c.k.katalon.core.main.TestCaseExecutor - SiteName = Mount Piper Power Station
2020-08-26 11:13:27.265 INFO c.k.katalon.core.main.TestCaseExecutor - fileName = Piper
2020-08-26 11:13:27.899 DEBUG testcase.Data Export Site - 1: downloadFilepath = getProjectDir() + “/File1/”
2020-08-26 11:13:28.091 DEBUG testcase.Data Export Site - 2: println(downloadFilepath)
C:/Katalon-Projects/SystemWeb/File1/
2020-08-26 11:13:28.099 DEBUG testcase.Data Export Site - 3: desiredCapabilities = getDriverPreferencesProperties(“WebUI”)
2020-08-26 11:13:28.103 DEBUG testcase.Data Export Site - 4: prefs = desiredCapabilities.get(“prefs”)
2020-08-26 11:13:28.117 DEBUG testcase.Data Export Site - 5: prefs.put(“download.default_directory”, downloadFilepath)
2020-08-26 11:13:28.119 DEBUG testcase.Data Export Site - 6: setWebDriverPreferencesProperty(“prefs”, prefs)
2020-08-26 11:13:28.124 DEBUG testcase.Data Export Site - 7: callTestCase(findTestCase(“General/Onsite_Login”), [:], STOP_ON_FAILURE)
2020-08-26 11:13:28.332 INFO c.k.katalon.core.main.TestCaseExecutor - --------------------
2020-08-26 11:13:28.332 INFO c.k.katalon.core.main.TestCaseExecutor - CALL Test Cases/General/Onsite_Login
2020-08-26 11:13:28.740 DEBUG testcase.Onsite_Login - 1: openBrowser(URLOnsite)
2020-08-26 11:13:29.159 INFO c.k.k.core.webui.driver.DriverFactory - Starting ‘Chrome’ driver
Aug 26, 2020 11:13:29 AM org.openqa.selenium.remote.DesiredCapabilities chrome
INFO: Using new ChromeOptions() is preferred to DesiredCapabilities.chrome()
2020-08-26 11:13:29.183 INFO c.k.k.c.w.util.WebDriverPropertyUtil - User set preference: [‘prefs’, ‘{download.default_directory=C:/Katalon-Projects/SystemWeb/File1/}’]
2020-08-26 11:13:29.224 INFO c.k.k.core.webui.driver.DriverFactory - Action delay is set to 0 milliseconds
Starting ChromeDriver 83.0.4103.39 (ccbf011cb2d2b19b506d844400483861342c20cd-refs/branch-heads/4103@{#416}) on port 10083
Only local connections are allowed.
Please see ChromeDriver - WebDriver for Chrome - Security Considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
[1598404412.771][WARNING]: This version of ChromeDriver has not been tested with Chrome version 84.
Aug 26, 2020 11:13:34 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
2020-08-26 11:13:34.902 INFO c.k.k.core.webui.driver.DriverFactory - sessionId = 7855807b7ca38e02390afa027b44868b
2020-08-26 11:13:34.954 INFO c.k.k.core.webui.driver.DriverFactory - browser = Chrome 84.0.4147.135
2020-08-26 11:13:34.955 INFO c.k.k.core.webui.driver.DriverFactory - platform = Windows 10
2020-08-26 11:13:34.955 INFO c.k.k.core.webui.driver.DriverFactory - seleniumVersion = 3.141.59
2020-08-26 11:13:34.957 INFO c.k.k.core.webui.driver.DriverFactory - proxyInformation = ProxyInformation { proxyOption=NO_PROXY, proxyServerType=HTTP, username=, password=********, proxyServerAddress=, proxyServerPort=0, executionList=“”, isApplyToDesiredCapabilities=true }
2020-08-26 11:13:35.646 DEBUG testcase.Onsite_Login - 2: maximizeWindow()
2020-08-26 11:13:37.805 DEBUG testcase.Onsite_Login - 3: click(findTestObject(“1. LOGGING ON AND HOME PAGE/Page_Onsite Login/form_Onsite LoginPlease enter”))
2020-08-26 11:13:38.262 DEBUG testcase.Onsite_Login - 4: setText(findTestObject(“1. LOGGING ON AND HOME PAGE/Page_Onsite Login/input_Username_user_id”), “testAT”)
2020-08-26 11:13:38.937 DEBUG testcase.Onsite_Login - 5: setEncryptedText(findTestObject(“1. LOGGING ON AND HOME PAGE/Page_Onsite Login/input_Password_password”), “DtV7fzKZcuQ2ALei4yjb3Q==”)
2020-08-26 11:13:39.412 DEBUG testcase.Onsite_Login - 6: click(findTestObject(“1. LOGGING ON AND HOME PAGE/Page_Onsite Login/input_Password_btnSubmit”))
2020-08-26 11:13:40.601 INFO c.k.katalon.core.main.TestCaseExecutor - END CALL Test Cases/General/Onsite_Login
2020-08-26 11:13:40.602 INFO c.k.katalon.core.main.TestCaseExecutor - --------------------
2020-08-26 11:13:40.608 DEBUG testcase.Data Export Site - 8: callTestCase(findTestCase(“6.UTILITY MENU/Small_TCs/DataExportSite”), [“SiteName”:SiteName, “fileName”:fileName], STOP_ON_FAILURE)
2020-08-26 11:13:40.670 INFO c.k.katalon.core.main.TestCaseExecutor - --------------------
2020-08-26 11:13:40.671 INFO c.k.katalon.core.main.TestCaseExecutor - CALL Test Cases/6.UTILITY MENU/Small_TCs/DataExportSite
2020-08-26 11:13:40.687 INFO c.k.katalon.core.main.TestCaseExecutor - SiteName = Mount Piper Power Station
2020-08-26 11:13:40.687 INFO c.k.katalon.core.main.TestCaseExecutor - fileName = Piper
2020-08-26 11:13:41.032 DEBUG testcase.DataExportSite - 1: waitForElementVisible(findTestObject(“Object Repository/6.UTILITY MENU/Edit_Logs/div_utility”), 20)
2020-08-26 11:13:41.507 DEBUG testcase.DataExportSite - 2: click(findTestObject(“Object Repository/6.UTILITY MENU/Edit_Logs/div_utility”))
2020-08-26 11:13:41.863 DEBUG testcase.DataExportSite - 3: click(findTestObject(“Object Repository/6.UTILITY MENU/DataExport/b_data export”))
2020-08-26 11:13:42.204 DEBUG testcase.DataExportSite - 4: click(findTestObject(“Object Repository/6.UTILITY MENU/DataExport/a_site”))
2020-08-26 11:13:42.925 DEBUG testcase.DataExportSite - 5: comment(“Site Data Export Page”)
2020-08-26 11:13:42.935 INFO c.k.k.c.keyword.builtin.CommentKeyword - Site Data Export Page
2020-08-26 11:13:42.936 DEBUG testcase.DataExportSite - 6: waitForElementVisible(findTestObject(“Object Repository/6.UTILITY MENU/DataExport/img_Site_handCursor”), 20)
2020-08-26 11:13:43.257 DEBUG testcase.DataExportSite - 7: click(findTestObject(“Object Repository/6.UTILITY MENU/DataExport/img_Site_handCursor”))
2020-08-26 11:13:43.887 DEBUG testcase.DataExportSite - 8: waitForElementVisible(findTestObject(“Object Repository/6.UTILITY MENU/DataExport/input_Select Site_searchStr”), 20)
2020-08-26 11:13:44.220 DEBUG testcase.DataExportSite - 9: setText(findTestObject(“Object Repository/6.UTILITY MENU/DataExport/input_Select Site_searchStr”), SiteName)
2020-08-26 11:13:44.903 DEBUG testcase.DataExportSite - 10: click(findTestObject(“Object Repository/6.UTILITY MENU/DataExport/Button_Search_Site_DataExp”))
2020-08-26 11:13:45.517 DEBUG testcase.DataExportSite - 11: waitForElementVisible(findTestObject(“Object Repository/6.UTILITY MENU/DataExport/a_SiteName”), 20)
2020-08-26 11:13:45.858 DEBUG testcase.DataExportSite - 12: click(findTestObject(“Object Repository/6.UTILITY MENU/DataExport/a_SiteName”))
2020-08-26 11:13:46.535 DEBUG testcase.DataExportSite - 13: waitForElementVisible(findTestObject(“6.UTILITY MENU/DataExport/input_Export Name_DataExp”), 20)
2020-08-26 11:13:46.881 DEBUG testcase.DataExportSite - 14: setText(findTestObject(“6.UTILITY MENU/DataExport/input_Export Name_DataExp”), fileName)
2020-08-26 11:13:47.518 DEBUG testcase.DataExportSite - 15: check(findTestObject(“Object Repository/6.UTILITY MENU/DataExport/chkbx_ContractoBlocks_dataToDo”))
2020-08-26 11:13:47.879 DEBUG testcase.DataExportSite - 16: click(findTestObject(“Object Repository/6.UTILITY MENU/DataExport/Button_Export”))
2020-08-26 11:13:51.147 DEBUG testcase.DataExportSite - 17: acceptAlert(OPTIONAL)

Here the code:
import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import static com.kms.katalon.core.testobject.ObjectRepository.findWindowsObject
import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint
import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.testcase.TestCase as TestCase
import com.kms.katalon.core.testdata.TestData as TestData
import com.kms.katalon.core.testng.keyword.TestNGBuiltinKeywords as TestNGKW
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import com.kms.katalon.core.windows.keyword.WindowsBuiltinKeywords as Windows
import internal.GlobalVariable as GlobalVariable
import com.kms.katalon.core.configuration.RunConfiguration as RunConfiguration

GlobalVariable.downloadFilepath = (RunConfiguration.getProjectDir() + ‘/File1/’)

println(GlobalVariable.downloadFilepath)

Map<String, Object> desiredCapabilities = RunConfiguration.getDriverPreferencesProperties(‘WebUI’)

Map<String, Object> prefs = desiredCapabilities.get(‘prefs’)

prefs.put(‘download.default_directory’, GlobalVariable.downloadFilepath)

RunConfiguration.setWebDriverPreferencesProperty(‘prefs’, prefs)

WebUI.callTestCase(findTestCase(‘General/Onsite_Login’), [:], FailureHandling.STOP_ON_FAILURE)

WebUI.callTestCase(findTestCase(‘6.UTILITY MENU/Small_TCs/DataExportSite’), [(‘SiteName’) : SiteName, (‘fileName’) : fileName],
FailureHandling.STOP_ON_FAILURE)

@pgonzalez
You need to add the these desired capabilities first:

{
  "prefs": {
    "download.default_directory": "Your download folder",
    "download.directory_upgrade": true,
    "download.prompt_for_download": false,
    "plugins.always_open_pdf_externally": true,
    "profile.default_content_settings.popups": "0",
    "profile.content_settings.exceptions.automatic_downloads.*.setting": "1"
  },
  "args": [
    "--no-sandbox",
    "--disable-dev-shm-usage"
  ],
  "useAutomationExtension": "false"
}

I pasted the code and changed “Your download folder” to the global variable

GlobalVariable.downloadFilepath = (RunConfiguration.getProjectDir() + ‘/File1/’)

println(GlobalVariable.downloadFilepath)

{
“prefs”: {
“download.default_directory”: GlobalVariable.downloadFilepath
“download.directory_upgrade”: true,
“download.prompt_for_download”: false,
“plugins.always_open_pdf_externally”: true,
“profile.default_content_settings.popups”: “0”,
“profile.content_settings.exceptions.automatic_downloads.*.setting”: “1”
},
“args”: [
“–no-sandbox”,
“–disable-dev-shm-usage”
],
“useAutomationExtension”: “false”
}

and got this:

Doesn’t let me run it. Any ideas?

To download the file clicking on the element with text as Download Data within this website you can use the following solution:

Code Block:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument("–headless")
options.add_argument("–window-size=1920,1080")
options.add_experimental_option(“excludeSwitches”, [“enable-automation”])
options.add_experimental_option(‘useAutomationExtension’, False)
driver = webdriver.Chrome(chrome_options=options, executable_path=r’C:\Utility\BrowserDrivers\chromedriver.exe’, service_args=["–log-path=./Logs/DubiousDan.log"])
print (“Headless Chrome Initialized”)
params = {‘behavior’: ‘allow’, ‘downloadPath’: r’C:\Users\Debanjan.B\Downloads’}
driver.execute_cdp_cmd(‘Page.setDownloadBehavior’, params)
driver.get(“https://www.mockaroo.com/”)
driver.execute_script(“scroll(0, 250)”);
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, “button#download”))).click()
print (“Download button clicked”)
#driver.quit()

Hello.

I am using this code, before openBrowser:

	Map prefs = [('intl.accept_languages') : GlobalVariable.locale,
		"download.default_directory": System.getProperty("user.home") + File.separator + "Downloads" + File.separator,
		"download.directory_upgrade": true,
		"download.prompt_for_download": false,
		"plugins.always_open_pdf_externally": true,
		"profile.default_content_settings.popups": "0",
		"profile.content_settings.exceptions.automatic_downloads.*.setting": "1",
		"devtools.preferences.cacheDisabled": "true"]
	
	Map <String, Map<String,String>> prefsFF = [('prefs'): prefs]
	RunConfiguration.setWebDriverPreferencesProperty("moz:firefoxOptions", prefsFF)
	RunConfiguration.setWebDriverPreferencesProperty('prefs', prefs)
	
	
	List<String> args = ['--ignore-certificate-errors', "--no-sandbox","--disable-dev-shm-usage"]
	RunConfiguration.setWebDriverPreferencesProperty('args', args)
	RunConfiguration.setWebDriverPreferencesProperty("useAutomationExtension", "false")

And it’s not working for Chrome headless. Any suggestions?

Hi @joana.pedroso . See this topic Downloading files not working anymore in Chrome Headless mode - #5 by joost.degeyndt.
From what I can tell, the issue for downloads on chrome headless will be fixed on m113, which should be released by Chrome Driver team near end of April. Until then, they suggest to change startup option from --headless to --headless=new.
I have no idea how to set this, as I think it is packed up within the Katalon browser startup scripts.

1 Like

The easisest approach will be to create a custom profile and to pass all needed flags for headless mode through capabilities, so instead of using “Chrome (headless)” provided by Katalon, just use this one.