XPath with randomized variables

Hello,
I’ve read various explanations of how variables are passed into xpath, but I’m still not getting it.

Object’s Xpath:
//*[@id='hello']/div/table/tbody[${index}]/tr/td[4]/span/a

My Script:
def index;
def x = ThreadLocalRandom.current().nextInt(1, 10 + 1)
WebUI.click(findTestObject('PageObject/a_Open', [(index) : x]))

parameter-xpath-a1

What am I doing wrong here? Simply trying to randomize the ‘tbody’ so that a random link is clicked in td[4].

Thanks,

You’re close! Try:

WebUI.click(findTestObject(‘PageObject/a_Open’, [‘index’ : x]))

(i.e. put ‘index’ in single quotes)

1 Like

Doesn’t seem to have worked.
I’m sure there’s something else I’m doing wrong.

Here’s the details:-

Test object:
a_Open

Selection Method:
XPath

Selector Editor:
//*[@id='hello']/div/table/tbody[${index}]/tr/td[4]/span/a

Test Script (excerpt):
def x = ThreadLocalRandom.current().nextInt(1, 10 + 1)
WebUI.click(findTestObject('PageObject/a_Open', ['index' : x]))

Test Object Variables:
parameter-xpath-a2

Is there possibly something else that I could be doing wrong?
If I replace XPath’s ‘[$index]’ with [1] or [6] or other integers, then it works fine. But the point is that I want to randomize ‘tbody’: not hard code values.

Thanks,

try to use selection mode attributes with xpath = ‘//*[@id=‘hello’]/div/table/tbody[${index}]/tr/td[4]/span/a’

i don’t know if it’s still true, but xpath selection method cannot be parameterized. (at least it was true few version back, i stopped to use selection method other then attributes back then)

1 Like

@Andrej_Podhajsky @Brandon_Hein
The combination of both your answers has helped me resolve this.

Katalon has such an active community.
Thank you!