Create Xpath with variable data

Hi,
I’m trying to create xpath using a variable that I create in my script.
As a simple example, I am using the following to create a unique value in the variable newName each time the script runs. I’d like to be able to plug that variable into an xpath later in the script.

def time = now.getTime()

String name = ‘Form’

String newName = name + time

I’d like to be able to use this variable as part of an xpath so that I can locate it in a table or other places on page, as in:
//input[@id=newName]

Can someone please advise on how to accomplish this or point me to the docs on it, which I can’t seem to find.
Thanks

It’s usually something like ${your_variable_name} in your xpath - but I’m not really an xpath guy so read through this Search Result:

https://forum.katalon.com/search?q=variables%20in%20xpath

1 Like

Here’s the doc:

Basic idea is this:

// Initialize newName
TestObject to = findTestObject('your testobject id', ['newName': newName]);
println to.getSelectorCollection().toString(); // Should see your XPath which contains the actual value of newName, among other stuffs

In your Test Object, the selected locator should be: //input[@id='${newName}']

1 Like

Thanks. I was able to get it working based on this example.