Text field for date wont accept setText

Hi All,

I have a text field for date which has a dynamic xpath with the unix timestamp included, hence when I try to execute setText on this text field, Katalon cannot find the element as the unix timestamp in the xpath gets updated.

Here is the xpath for reference-
//*[@id=“dt_1_1741752232482”]

Is there a way to handle this xpath so that Katalon will be able to detect this text field and input data?

Any help would be much appreciated!

use as below

//*[contains(@id, 'dt_1_')]
1 Like

Is there a label near the date field? If there is, then you can use the label’s text and “move” from it to the date field to create an unique pathway without using the dynamic <id>. There are functions for XPath to move up a node, down a node, move up or down to another sibling tab, etc. So what does the HTML neighbourhood look like?
A sample pathway using functions:
//span[text()="Report Date"]/parent::div/following-sibling::div/input

Here’s how to handle dynamic XPaths with changing timestamps (e.g., id="dt_1_1741752232482") in Katalon:

Solution 1: Use a Dynamic XPath with starts-with

Modify the XPath to target the static portion of the ID (e.g., dt_1_):

//*[starts-with(@id, 'dt_1_')]

Steps:

  1. In Katalon’s Object Repository, edit the element’s XPath to:
//*[starts-with(@id, 'dt_1_')]
  1. Use this object in your script:
WebUI.setText(testObject, "01/01/2024")

Solution 2: Use CSS Selectors (if applicable)

For IDs that start with dt_1_, use a CSS selector:

[id^='dt_1_']

Set this as the element’s CSS Selector in the Object Repository.

Solution 3: Use Relative XPath with Other Attributes

If the element has other static attributes (e.g., name, placeholder, or class), combine them with the dynamic ID:

//input[starts-with(@id, 'dt_1_') and @placeholder='Enter Date']

Solution 4: Use Regular Expressions (via matches())

If your XPath engine supports regex (limited in older browsers), use:

//*[matches(@id, 'dt_1_\d+')]

This matches dt_1_ followed by digits.

Solution 5: Dynamic XPath in Script (Programmatic Approach)

Construct the XPath dynamically in code:

String dynamicId = "dt_1_" + System.currentTimeMillis() // Example timestamp logic
WebElement element = DriverFactory.getWebDriver().findElement(By.xpath("//*[@id='${dynamicId}']"))
WebUI.setText(element, "01/01/2024")

Best Practices

  1. Add Explicit Waits to ensure the element is interactable:
WebUI.waitForElementPresent(testObject, 10)
  1. Verify Uniqueness: Ensure your dynamic XPath matches only one element.
  2. Test in Spy Web: Validate the XPath in Katalon’s Spy Web Utility before scripting.

By using starts-with or CSS selectors, Katalon will reliably locate the element even with dynamic timestamps

1 Like

thanks a lot mate, this worked :+1:

2 Likes