Chromeoptions for headless is not working

Hi, When I run my script in headless the elements are not identified and not clickable. So I tried to add chromeoptions in both in Project settings and also in the script but still its not working and I assume my chromeoptions might not have set correctly.

import org.openqa.selenium.WebDriver
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.chrome.ChromeOptions

import com.kms.katalon.core.webui.driver.DriverFactory
import com.kms.katalon.core.webui.driver.WebUIDriverType
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
WebUI.openBrowser(’’)

Tried this option and didn’t work so moved to next one

//final ChromeOptions options = new ChromeOptions();
//options.addArguments("–start-maximized");
//options.addArguments(’–headless’)
//options.addArguments(’–disable-gpu’)
//options.addArguments(‘no-sandbox’)
////options.addArguments(“start-maximized”)
//options.addArguments("–window-size=1920x1080")
//ChromeDriver driver = new ChromeDriver(options);

Next option that I tried

if (DriverFactory.getExecutedBrowser() == WebUIDriverType.HEADLESS_DRIVER) {

System.setProperty(‘webdriver.chrome.driver’,
DriverFactory.getChromeDriverPath())
ChromeOptions options = new ChromeOptions()
options.addArguments(’–headless’, ‘–disable-gpu’, ‘–start-maximized’, ‘window-size=1920,1080’)
WebDriver driver = new ChromeDriver(options)
DriverFactory.changeWebDriver(driver)
DriverFactory.changeWebDriver(driver)
}

WebUI.openBrowser(’’)
WebUI.navigateToUrl(‘https://test.digitalstudio.io/’)
WebDriver driver = DriverFactory.getWebDriver()
driver.findElement(menuListObjLocator).click()

In project>settings>Desired Capabilities>Web UI>Chrome (headless)
[‘args’, [’"–headless","–window-size=1920,1080"’]]

Any help would be greatly appreciated.

hi,

this will work with chrome headless

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();
}
1 Like

Hi @Timo_Kuisma1,
Thanks for the code. I tried this code and seems like its working as is. But what I was looking for is I need to use both combinations of WebUI and Webdriver methods as per my script goes. May I know where should I initiate the WebUI and use that as my driverfactory. Also I will be using multiple testcases to run End to End scenarios. So I will need the same driver used across the suite.
Thanks

At last, I am able to manage and now the script is working as I needed.