Use variable in xpath

Thank you for the help

kazurayam said:

It seems that backslash before single quotation is not necessary. PLS try this://*[normalize-space(text())='${dashboard}']

Thanks, works like a charm!

Hi,

what I’m doing wrong? I tried the whole day, with css and with xpath property, but didn’t get this approach to work with Katalon Studio 5.4.2. As long as I use hard coded values for the active xpath statement of my TestObject (with Basic Selection Method) like:

//td[3]

or even for a accordingly css (Basic) statement like:

*:nth-of-type(3)>td

… everything is fine for a TestCase that accesses such TestObject in a way like:

println WebUI.getText(findTestObject(myTestObjectId))

That means, in both cases I get the 3rd td element in DOM.

But if the TestObject’s xpath property refers to a local variable (say webElmId = 3) like:

//td['${webElmId}']

… there will be found only the first occurence of td in DOM. It’s exactly like if I said “//td[1]”.

And if the TestObject’s css property refers to the same local variable like:

*:nth-of-type('${webElmId}')>td

… of course, nothing will be found.

Am I doing something wrong? Or was there a change in the way Katalon Studio evaluates these TestObject properties?

Thanks a lot and regards!

**# Solution **

Please change your XPath expression as this:

//td[${webElmId}]

and try to see what you get.

# Description

You have this now:

//td['${webElmId}']

Katalon Studio transforms this expression to

//td['3']

This expression is equal to:

//td[position()='3']

The XPath Engine (jaxen built-in Katalon Studio) will evaluate the predicate [position()=‘3’] to false because ‘3’ is not a number. The function position() returns a number value. On the other hand, ‘3’ is a string. In XPath, a comparison of a number and a string makes false.

Then the above expression will result in the same value as

//td

XPath Engine offers 2 interpretation of this expression as follows:

  1. a node-set of elements in the HTML document
  2. the 1st element among a node-set of elements in the HTML document

    Katalon Studio takes the 2nd interpretation. Therefore you get the same value as “//td[1]”.

3 Likes

Hi kazurayam,

once again very conclusively deduced, thank you for your effort! But unfortunately, I had only forgotten to mention that, it doesn’t work for me without quotes either. The result of my command:

println WebUI.getText(findTestObject(myTestObjectId))

… in the case of using xpath is the same as with or without single quotes in the CSS statement:

[FAILED] - Unable to get text of object ‘Object Repository/myTestObject’ (Root cause: com.kms.katalon.core.webui.exception.WebElementNotFoundException: …

It almost seems to me that my variable webElmId is empty? But the TestCase log even says at the beginning:

Variable 'webElmId' is set to 3 as default

BTW, is my expectation actually correct that I should be able to change this local variable to a different integer value during the TestCase runtime in order to use again and again:

println WebUI.getText(findTestObject(myTestObjectId))

… for access to the next TestObject within my WebElement-List of further matching td elements in DOM?

Thank you!

Your Test Case snippet is this:

println WebUI.getText(findTestObject(myTestObjectId))

This is not enough.

The following code would work:

println WebUI.getText(findTestObject(myTestObjectId, ['webElmId' : 3]))

See the API doc of ObjectRepository#findTestObject(String, Map)

2 Likes

Genius, that was my fault! :slight_smile: I somehow assumed without checking that the object properties would have direct access to the (local) variables used in the respective TestCase. Thank you very much for illuminating that! :slight_smile:

HI,

I am trying to use below expression

findTestObject(’//a[text() = ‘${sElement’}’)

here sElement is variable, I tried with and without single quotes but still unable to find object. If I use individual object it’s working fine. I tried similar Xpath with double quotes like below

findTestObject("//a[text() = ${sElement}"), still it’s not working. I want to use dynamic Xpath. Please tell me how can we use dynamic Xpath

answer here : Unable to use dynamic Xpath

@kazurayam - Can you help me on this.
This is the xpath with value directly on it
//li[@id=‘Car’]
Trying to replace the above by
//li[@id=’${loanPurpose}’]

Caused by: com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: ‘Object Repository/Page/auto’ located by ‘By.xpath: //li[@id=’${loanPurpose}’]’ not found

Getting the above error

How does your test case script looks like? Does it have a line which interpolates the placeholder ${loanPurpose} with a value Car, like:

println WebUI.getText(findTestObject(myTestObjectId, ['loanPurpose' : 'Car']))

Hi,

This is my xpath for table data: //tr[3]/td[3]/div
i need to write dynamic xpath . Could you please advise how i need to declare variable and how shall i code xpath .

I declared variable i =3 and j =3
and gave xpath like //tr[${i}]/td[${j}]/div but it didnt work. pls advise

How does your test case script looks like? Does it have a line which interpolates the placeholder ${i} with a value 3 and ${j} with a value 3, like:

println WebUI.getText(findTestObject(myTestObjectId, ['i' : 3, 'j': 3]))

After adding the above line as placeholder $, can i code in test object as

//tr[${i}/td[${j}]

Pls advise

@ThanhTo

Please improve the explanation in the Katalon Documentation in

Many people ask the same question repeatedly.

They understand that

(1) they need to write a xpath with placeholder: //tr[${i}]/td[${j}]/div

(2) they need to set values to the variables i and j.

But they do not understand that

(3) they need to call findTestObject method with 2nd argument: a list of [placeholderName: value] pairs.

println WebUI.getText(findTestObject(myTestObjectId, ['i' : 3, 'j': 3]))

The official documentation is too long, so they would not read it. The doc explains the point (1), but it is not explicit enough about (3). It has too few examples.

1 Like

I could find a section on Parameterizing Mobile Test Object.

I could find a section on Parameterizing Web Service Object.

But now I could find no section on Parameterizing Web UI Test Object. Have you removed it?

@eug

It seems you have the following fragment in your test object

//tr[${i}/td[${j}]

This is wrong. One ] character is missing. It should be:

//tr[${i}]/td[${j}]
1 Like

Hi @kazurayam

I will update the Parameterize Mobile Test Object document accordingly. I appreciate your feedback.

The Parameterize web test object document is combined with the Web test object document. Considered it’s difficult for users to search for it, I will change it. Thank you.

Jass

Thanks . I was able to execute my test script. :slight_smile: