# Use variable in xpath

**URL:** <https://forum.katalon.com/t/use-variable-in-xpath/9398>\
**Category:** Web Testing\
**Tags:** katalon-studio\
**Created:** [February 15, 2018, 10:10am UTC](https://forum.katalon.com/t/use-variable-in-xpath/9398 "2018-02-15T10:10:53Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Test\_Q](https://avatars.discourse-cdn.com/v4/letter/t/90ced4/32.png) [@Test\_Q](https://forum.katalon.com/u/Test_Q)\
**Post date:** [February 15, 2018, 10:10am UTC](https://forum.katalon.com/t/use-variable-in-xpath/9398/1 "2018-02-15T10:10:53Z")

</div>

Hi there,

Currently I’m struggling with putting a variable into xpath to make the object detection dynamic.

The xpath which identifies the object(a\_dashboarditems): //\*[normalize-space(text())=‘Projects’]  
[object.PNG](https://s3.amazonaws.com/katalon-forum/editor/tr/w2jvblxasmkz.png "Link: https://s3.amazonaws.com/katalon-forum/editor/tr/w2jvblxasmkz.png")  
This works perfectly fine. I can adjust the name and it will also find other items

To make it dynamic, I made a variable on the variable tab called “dashboard”, with the default value: “Projects”  
[variabletab.PNG](https://s3.amazonaws.com/katalon-forum/editor/py/9fysmazjjo5t.png)

I modified the xpath value at the object in a few ways:  
//_[normalize-space(text(),${dashboard})]  
//_[normalize-space(text())=${dashboard}]

Unfortunately the options above don’t work, the object cannot be find and clicked on anymore.  
As an extra step I want to make this variable tab using an excel sheet, but this easy once the step above is working.

Can someone point me into the right direction to make this work? A solution would also be nice.

Thanks!

[object.PNG](https://s3.amazonaws.com/katalon-forum/editor/tr/w2jvblxasmkz.png)

[variabletab.PNG](https://s3.amazonaws.com/katalon-forum/editor/py/9fysmazjjo5t.png)

---

<div class="post-metadata">

**Author:** ![Test\_Q](https://avatars.discourse-cdn.com/v4/letter/t/90ced4/32.png) [@Test\_Q](https://forum.katalon.com/u/Test_Q)\
**Post date:** [February 15, 2018, 10:17am UTC](https://forum.katalon.com/t/use-variable-in-xpath/9398/2 "2018-02-15T10:17:15Z")

</div>

Seems I cant edit my first post, but there’s a typo in the object.png screenshot: “projectbeheer” should be “projects” in the xpath.

The error message:

Test Cases/Test FAILED because (of) Unable to click on object ‘Object Repository/a\_dashboarditems’ (Root cause: com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: ‘Object Repository/a\_dashboarditems’ located by ‘By.xpath: //\*[normalize-space(text(),${dashboard})]’ not found)

Test Cases/Test.run:26

---

<div class="post-metadata">

**Author:** ![Test\_Q](https://avatars.discourse-cdn.com/v4/letter/t/90ced4/32.png) [@Test\_Q](https://forum.katalon.com/u/Test_Q)\
**Post date:** [February 16, 2018, 9:35am UTC](https://forum.katalon.com/t/use-variable-in-xpath/9398/3 "2018-02-16T09:35:09Z")

</div>

I managed to get the first step working, putting a variable in the xpath, by using the script tab:

dashboard = ‘Projects’

TestObject to = findTestObject(‘a\_dashboarditems’)

to.findProperty(‘xpath’).setValue(’//\*[normalize-space(text())=\’’+dashboard+’\’]’)

But i can’t get it to work in the object’s property screen. I need to get this to work because i want to use a variable from a csvfile later on.  
Anyone knows how to use the variable in xpath by using the objectproperty tab?

---

<div class="post-metadata">

**Author:** ![Jaikumar\_Kesav](https://avatars.discourse-cdn.com/v4/letter/j/848f3c/32.png) [@Jaikumar\_Kesav](https://forum.katalon.com/u/Jaikumar_Kesav)\
**Post date:** [April 30, 2018, 12:54pm UTC](https://forum.katalon.com/t/use-variable-in-xpath/9398/4 "2018-04-30T12:54:26Z")

</div>

hey, i am looking something like this, can you please explain with more screenshots

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [April 30, 2018, 9:45pm UTC](https://forum.katalon.com/t/use-variable-in-xpath/9398/5 "2018-04-30T21:45:55Z")

</div>

Could you try this in the Test Object definition for a\_dashboarditems:

```auto
//*[normalize-space(text())='${dashboard}']

```

You need to quote the string value of ${dashboard}.

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [May 1, 2018, 12:44am UTC](https://forum.katalon.com/t/use-variable-in-xpath/9398/6 "2018-05-01T00:44:14Z")

</div>

It seems that backslash before single quotation is not necessary.  
PLS try this:

```auto
//*[normalize-space(text())='${dashboard}']

```

---

<div class="post-metadata">

**Author:** ![Jaikumar\_Kesav](https://avatars.discourse-cdn.com/v4/letter/j/848f3c/32.png) [@Jaikumar\_Kesav](https://forum.katalon.com/u/Jaikumar_Kesav)\
**Post date:** [May 7, 2018, 12:27pm UTC](https://forum.katalon.com/t/use-variable-in-xpath/9398/7 "2018-05-07T12:27:42Z")

</div>

Thank you for the help

---

<div class="post-metadata">

**Author:** ![Test\_Q](https://avatars.discourse-cdn.com/v4/letter/t/90ced4/32.png) [@Test\_Q](https://forum.katalon.com/u/Test_Q)\
**Post date:** [May 17, 2018, 8:08am UTC](https://forum.katalon.com/t/use-variable-in-xpath/9398/8 "2018-05-17T08:08:30Z")

</div>

> [kazurayam](https://forum.katalon.com/profile/4280/kazurayam) said:
> 
> ```auto
> It seems that backslash before single quotation is not necessary. PLS try this://*[normalize-space(text())='${dashboard}']
> 
> ```

Thanks, works like a charm!

---

<div class="post-metadata">

**Author:** ![Drunda\_Nibel](https://avatars.discourse-cdn.com/v4/letter/d/bb73d2/32.png) [@Drunda\_Nibel](https://forum.katalon.com/u/Drunda_Nibel)\
**Post date:** [June 6, 2018, 10:29pm UTC](https://forum.katalon.com/t/use-variable-in-xpath/9398/9 "2018-06-06T22:29:56Z")

</div>

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:

```auto
//td[3]

```

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

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

```

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

```auto
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:

```auto
//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:

```auto
*: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!

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [June 7, 2018, 12:05am UTC](https://forum.katalon.com/t/use-variable-in-xpath/9398/10 "2018-06-07T00:05:11Z")

</div>

\*\*# Solution \*\*

Please change your XPath expression as this:

```auto
//td[${webElmId}]

```

and try to see what you get.

**# Description**

You have this now:

```auto
//td['${webElmId}']

```

Katalon Studio transforms this expression to

```auto
//td['3']

```

This expression is equal to:

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

```

The XPath Engine ([jaxen](https://github.com/jaxen-xpath/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

```auto
//td

```

XPath Engine offers 2 interpretation of this expression as follows:

1. a node-set of elements in the HTML document
2. Katalon Studio takes the 2nd interpretation. Therefore you get the same value as “//td[1]”.

---

<div class="post-metadata">

**Author:** ![Drunda\_Nibel](https://avatars.discourse-cdn.com/v4/letter/d/bb73d2/32.png) [@Drunda\_Nibel](https://forum.katalon.com/u/Drunda_Nibel)\
**Post date:** [June 7, 2018, 6:52am UTC](https://forum.katalon.com/t/use-variable-in-xpath/9398/11 "2018-06-07T06:52:26Z")

</div>

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:

```auto
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:

```auto
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:

```auto
println WebUI.getText(findTestObject(myTestObjectId))

```

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

Thank you!

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [June 7, 2018, 7:33am UTC](https://forum.katalon.com/t/use-variable-in-xpath/9398/12 "2018-06-07T07:33:12Z")

</div>

Your Test Case snippet is this:

```auto
println WebUI.getText(findTestObject(myTestObjectId))

```

This is not enough.

The following code would work:

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

```

See the API doc of [ObjectRepository#findTestObject(String, Map)](https://api-docs.katalon.com/studio/v4.7.0/api/com/kms/katalon/core/testobject/ObjectRepository.html#findTestObject(java.lang.String,%20Map%3CObject,%20Object%3E) "Link: https://api-docs.katalon.com/studio/v4.7.0/api/com/kms/katalon/core/testobject/ObjectRepository.html#findTestObject(java.lang.String,%20Map%3CObject,%20Object%3E)")

---

<div class="post-metadata">

**Author:** ![Drunda\_Nibel](https://avatars.discourse-cdn.com/v4/letter/d/bb73d2/32.png) [@Drunda\_Nibel](https://forum.katalon.com/u/Drunda_Nibel)\
**Post date:** [June 7, 2018, 7:54am UTC](https://forum.katalon.com/t/use-variable-in-xpath/9398/13 "2018-06-07T07:54:07Z")

</div>

Genius, that was my fault! 🙂 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! 🙂

---

<div class="post-metadata">

**Author:** ![shubham.it09](https://avatars.discourse-cdn.com/v4/letter/s/ad7895/32.png) [@shubham.it09](https://forum.katalon.com/u/shubham.it09)\
**Post date:** [February 20, 2019, 9:17am UTC](https://forum.katalon.com/t/use-variable-in-xpath/9398/14 "2019-02-20T09:17:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Andrej\_Podhajsky](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/andrej_podhajsky/32/1055_2.png) [@Andrej\_Podhajsky](https://forum.katalon.com/u/Andrej_Podhajsky)\
**Post date:** [February 20, 2019, 9:31am UTC](https://forum.katalon.com/t/use-variable-in-xpath/9398/15 "2019-02-20T09:31:54Z")

</div>

answer here : [Unable to use dynamic Xpath](http://forum.katalon.com/t/unable-to-use-dynamic-xpath/19697)

---

<div class="post-metadata">

**Author:** ![radhikaperumal](https://avatars.discourse-cdn.com/v4/letter/r/8c91f0/32.png) [@radhikaperumal](https://forum.katalon.com/u/radhikaperumal)\
**Post date:** [September 28, 2019, 2:11am UTC](https://forum.katalon.com/t/use-variable-in-xpath/9398/16 "2019-09-28T02:11:58Z")

</div>

@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

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [September 28, 2019, 3:25am UTC](https://forum.katalon.com/t/use-variable-in-xpath/9398/17 "2019-09-28T03:25:01Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![eug](https://avatars.discourse-cdn.com/v4/letter/e/e9bcb4/32.png) [@eug](https://forum.katalon.com/u/eug)\
**Post date:** [June 2, 2020, 2:36pm UTC](https://forum.katalon.com/t/use-variable-in-xpath/9398/18 "2020-06-02T14:36:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [June 2, 2020, 2:53pm UTC](https://forum.katalon.com/t/use-variable-in-xpath/9398/19 "2020-06-02T14:53:43Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![eug](https://avatars.discourse-cdn.com/v4/letter/e/e9bcb4/32.png) [@eug](https://forum.katalon.com/u/eug)\
**Post date:** [June 2, 2020, 3:03pm UTC](https://forum.katalon.com/t/use-variable-in-xpath/9398/20 "2020-06-02T15:03:35Z")

</div>

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

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

Pls advise

[Next page](https://forum.katalon.com/t/use-variable-in-xpath/9398.md?page=2)
