When I ran my script without headless it works fine but in headless mode the screen gets smaller which gives me error and not able to click the ok button since is out of the screen.
Hi there, and thanks for posting in the Katalon community!
To help you faster, please review our guide on Chrome Headless here:
- Headless Browsers Execution in Katalon Studio | Katalon Docs.
- https://katalon-inc.my.site.com/katalonhelpcenter/s/article/What-are-the-best-practices-or-tips-when-working-with-headless-browsers.
Double-checking the steps and configurations might resolve the issue.
If the doc doesn’t help, feel free to provide more details, and a community member will assist you soon.
Thanks for being a part of our community!
Best,
Elly Tran
Hi @snishi,
Can you please help provide code, error log, version, web or mobile testing… so that we can better support? I would love you to use scrollToElement and tapAtPosition with some delays/waits so that the element can be clickable. Hope this can help. Thank you
To resolve the element click issue in Chrome headless mode due to a smaller viewport, follow these steps:
1. Set a Larger Window Size in Headless Mode
Adjust the default window size using Chrome options to ensure elements are visible and clickable.
In Katalon Studio:
- Open your test profile (e.g., Headless) in Execution Settings.
- Add the following Chrome options under WebUI > Chrome:
groovy
--window-size=1920,1080 // Sets the window to 1920x1080 pixels
--start-maximized // Maximizes the window (redundant with --window-size but added for safety)
Example configuration:
groovy
["headless", "disable-gpu", "window-size=1920,1080", "start-maximized"]
2. Programmatically Set Window Size
Add a step in your script to enforce the window size even if Chrome ignores arguments:
groovy
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
WebUI.openBrowser('')
WebUI.setViewPortSize(1920, 1080) // Force the viewport size
WebUI.navigateToUrl('https://your-url.com')
3. Scroll to the Element Before Clicking
Ensure the element is in the viewport using WebUI.scrollToElement
:
groovy
WebUI.scrollToElement(findTestObject('Your_Ok_Button'), 10)
WebUI.click(findTestObject('Your_Ok_Button'))
4. Use JavaScript Click as a Fallback
If the UI click fails, use JavaScript to trigger the click:
groovy
import com.kms.katalon.core.webui.common.WebUiCommonHelper
TestObject okButton = findTestObject('Your_Ok_Button')
WebUiCommonHelper.executeJavaScript("arguments[0].click();", WebUiCommonHelper.findWebElement(okButton))
5. Verify Headless-Specific Rendering
Some elements (e.g., modals) may render differently in headless mode. Test for headless-specific conditions:
groovy
if (WebUI.getWebDriver().getCapabilities().getCapability("browserName").equals("chrome") &&
WebUI.getWebDriver().getCapabilities().getCapability("headless").equals(true)) {
// Add headless-specific logic (e.g., adjust element locators)
}
Example Final Script
groovy
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
// Start browser with headless arguments
WebUI.openBrowser('')
WebUI.setViewPortSize(1920, 1080)
WebUI.navigateToUrl('https://your-url.com')
// Scroll to the button and click
WebUI.scrollToElement(findTestObject('Your_Ok_Button'), 10)
WebUI.click(findTestObject('Your_Ok_Button'))
// Optional: JavaScript click if UI click fails
if (!WebUI.verifyElementClickable(findTestObject('Your_Ok_Button'), FailureHandling.OPTIONAL)) {
WebUI.executeJavaScript('arguments[0].click()', [WebUiCommonHelper.findWebElement(findTestObject('Your_Ok_Button'))])
}
Why This Works
- Window Size: Headless Chrome defaults to 800x600. Overriding this ensures elements are within the viewport.
- Scrolling: Explicitly scrolls to the element to mimic real-user interaction.
- JavaScript Fallback: Bypasses UI limitations in headless environments.
I have been running my automation scripts in Chrome headless mode (locally and on Jenkins) for over a year without issues, but recently elements like the “OK” button are no longer clickable because they fall outside a now smaller viewport. This only happens in headless mode everything works in normal mode. I have tried setting --window-size=1920,1080 and adjusting the view port programmatically, but Chrome (Version 137.0.7151.104) seems to ignore these settings. I also confirmed the issue doesn’t happen in Firefox headless, where screenshots show a correct 1920x1080 resolution, while Chrome’s screenshots are noticeably smaller and I haven’t made any changes to the scripts or environment. I am hoping for a proper solution, as using JavaScript clicks or scrolling would create maintenance challenge across the entire test suite.
The command line option --window-size=w,h
seems to be broken in the recent ChromeDriver.
Please try alternative ways of setting the window size.
driver.set_window_size(w, h) helped us to resolve the issue.
from ChromeDriver not honoring the --window-size parameter #15827