Trying to read value from input field using gettext but not working

I’m trying to automate a checkout flow in Katalon Studio, and I need to capture the value of the “Total Price” field to verify the calculations are correct.

I recorded my test script and used the standard WebUI.getText(findTestObject('Page_Checkout/input_TotalPrice')) step. But every time I run the script, it returns an empty string "" in the console, even though I can clearly see “$150.00” typed inside the box on my screen!

I tried adding a WebUI.waitForElementVisible before it, thinking maybe it was a loading issue, but that didn’t change anything. I also inspected the element in Chrome, and the text isn’t sitting between <span> or <div> tags; it’s inside an <input> tag. I’m completely stuck on how to grab this text if getText() doesn’t want to work. What am I doing wrong?

This might be because the values are stored as an attribute in the DOM and not directly as text.

Here is an example

If you see, here the text in the field is stored as attribute ‘value’ in the DOM and not as plain text.

So you need to get the value attribute of that element and then compare with your expected value.

WebUI.getAttribute(findTestObject(ELEMENT), ‘value’)

If this does not solve your problem, pls share the DOM structure of your element so the community can further help you.

WebUI.getText() returns empty on an <input> because the visible value lives in the element’s value attribute, not as inner text. Use WebUI.getAttribute(..., 'value') instead.

Use This

String total = WebUI.getAttribute(findTestObject('Page_Checkout/input_TotalPrice'), 'value')
println total

Verify It

WebUI.verifyElementAttributeValue(
    findTestObject('Page_Checkout/input_TotalPrice'),
    'value',
    '$150.00',
    10
)

Why waitForElementVisible didn’t help

Visibility only confirms the element is on the page; it does not change whether the value is stored as text or as an attribute. For <input> fields, reading value is the correct approach.

What you are experiencing is one of the most common hurdles when transitioning from manual testing to automation, so don’t worry—your logic is sound, you’re just using the wrong tool for this specific HTML structure.

What is Going Wrong?

In web development, different HTML elements store their visible content differently:

  • Standard text elements (like <div>, <span>, <p>, or <td>) store their text as innerHTML (between the opening and closing tags). This is what Katalon’s WebUI.getText() is designed to read.

  • Form elements (like <input>, <textarea>, or text boxes) store their displayed text inside a hidden runtime property called value. Because the text isn’t actually between standard HTML tags, WebUI.getText() looks inside and finds absolutely nothing, returning a blank string.

The Simple Solution

To get text from an input field in Katalon Studio, you need to use WebUI.getAttribute() instead of WebUI.getText(), and specifically ask for the "value" attribute.

Instead of:

Groovy

String totalPrice = WebUI.getText(findTestObject('Page_Checkout/input_TotalPrice'))

Use this:

Groovy

String totalPrice = WebUI.getAttribute(findTestObject('Page_Checkout/input_TotalPrice'), 'value')

Reusable Custom Keyword Solution

To ensure your test scripts stay clean, scalable, and easy for anyone on your team to use without remembering this HTML quirk, you can create a Custom Keyword. This keyword acts as a smart wrapper that automatically falls back to fetching the input value if standard text returns empty.

Step 1: Create the Custom Keyword

Create a new Keyword package/class (e.g., com.helper.SmartWebUI) and paste the following code:

Groovy

package com.helper

import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

public class SmartWebUI {

	/**
	 * Dynamically fetches text from an element, automatically handling standard text tags and input fields.
	 * @param testObject The Katalon Test Object to extract text from.
	 * @return String containing the element's text or input value.
	 */
	@Keyword
	def static String getElementText(TestObject testObject) {
		// First, try the standard getText() method
		String visibleText = WebUI.getText(testObject)
		
		// If it's empty, check if it's an input field by grabbing the 'value' attribute
		if (visibleText == null || visibleText.trim().isEmpty()) {
			visibleText = WebUI.getAttribute(testObject, 'value')
		}
		
		return visibleText
	}
}

Step 2: Use it in your Test Script (Script Mode)

Now, in your test cases, you can call this custom keyword seamlessly:

Groovy

// Call your custom keyword instead of standard WebUI
String totalPrice = CustomKeywords.'com.helper.SmartWebUI.getElementText'(findTestObject('Page_Checkout/input_TotalPrice'))

// Now you can safely print or verify the value!
WebUI.comment("The captured price is: " + totalPrice)