# Stuggling with passing variable to object's xpath

**URL:** <https://forum.katalon.com/t/stuggling-with-passing-variable-to-objects-xpath/8601>\
**Category:** Web Testing\
**Tags:** katalon-studio\
**Created:** [December 15, 2017, 1:17pm UTC](https://forum.katalon.com/t/stuggling-with-passing-variable-to-objects-xpath/8601 "2017-12-15T13:17:01Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gerlof\_Hoekstra](https://avatars.discourse-cdn.com/v4/letter/g/bb73d2/32.png) [@Gerlof\_Hoekstra](https://forum.katalon.com/u/Gerlof_Hoekstra)\
**Post date:** [December 15, 2017, 1:17pm UTC](https://forum.katalon.com/t/stuggling-with-passing-variable-to-objects-xpath/8601/1 "2017-12-15T13:17:01Z")

</div>

Hello,  
Just starting to learn Katalon Studio: I feel it is a great tool and I am getting really enthousiastic!

However, I’m struggling with dynamic xpath expressions. I want to adress a row in a table using a variable.

I have an object in the object repository with the following xpath:  
\_ id(“booked\_form”)/table[[@class=“login”]/tbody[1]/tr[2]/td[1]/table[1]/tbody[1]/tr[](http://forum.katalon.com/profile/class%3D%22login%22%5D%2Ftbody%5B1%5D%2Ftr%5B2%5D%2Ftd%5B1%5D%2Ftable%5B1%5D%2Ftbody%5B1%5D%2Ftr%5B "Link: http://forum.katalon.com/profile/class%3D%22login%22%5D%2Ftbody%5B1%5D%2Ftr%5B2%5D%2Ftd%5B1%5D%2Ftable%5B1%5D%2Ftbody%5B1%5D%2Ftr%5B")**5]**/td[1]  
\_and like to replace the _ **5** _ with a variable name.  
Any hints how do I do that?

Kind regards,

Gerlof ![](https://s3.amazonaws.com/katalon-forum/editor/6z/c5e7fmftqcp0.png "Image: https://s3.amazonaws.com/katalon-forum/editor/6z/c5e7fmftqcp0.png")

[Capture.PNG](https://s3.amazonaws.com/katalon-forum/editor/6z/c5e7fmftqcp0.png)

---

<div class="post-metadata">

**Author:** ![nhi](https://avatars.discourse-cdn.com/v4/letter/n/d2c977/32.png) [@nhi](https://forum.katalon.com/u/nhi)\
**Post date:** [December 15, 2017, 1:58pm UTC](https://forum.katalon.com/t/stuggling-with-passing-variable-to-objects-xpath/8601/2 "2017-12-15T13:58:38Z")

</div>

@“Gerlof Hoekstra”: You might use manage test object by parameterizing its property.  
This guidance introduces to use it: [https://docs.katalon.com/display/KD/Manage+Test+Object#ManageTestObject-ParameterizingTestObject](https://docs.katalon.com/display/KD/Manage+Test+Object#ManageTestObject-ParameterizingTestObject "Link: https://docs.katalon.com/display/KD/Manage+Test+Object#ManageTestObject-ParameterizingTestObject")

---

<div class="post-metadata">

**Author:** ![Gerlof\_Hoekstra](https://avatars.discourse-cdn.com/v4/letter/g/bb73d2/32.png) [@Gerlof\_Hoekstra](https://forum.katalon.com/u/Gerlof_Hoekstra)\
**Post date:** [December 15, 2017, 4:04pm UTC](https://forum.katalon.com/t/stuggling-with-passing-variable-to-objects-xpath/8601/3 "2017-12-15T16:04:17Z")

</div>

Thanks for your suggestion. I did read that guide but cannot get it to work. Maybe I do not understand. In the test case, I created parameter RowNr:

![](https://s3.amazonaws.com/katalon-forum/editor/3o/55rzjp9si0cn.png "Image: https://s3.amazonaws.com/katalon-forum/editor/3o/55rzjp9si0cn.png")

Then I think I have to link this someway to the object in the object repository, but I think here I do not understand it quite well, now I have a failure during playback. What am I doing wrong?

![](https://s3.amazonaws.com/katalon-forum/editor/w5/1cx55mqm7sbq.png "Image: https://s3.amazonaws.com/katalon-forum/editor/w5/1cx55mqm7sbq.png")

[Capture1.PNG](https://s3.amazonaws.com/katalon-forum/editor/3o/55rzjp9si0cn.png)

[Capture2.PNG](https://s3.amazonaws.com/katalon-forum/editor/w5/1cx55mqm7sbq.png)

---

<div class="post-metadata">

**Author:** ![Ana\_Hong](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@Ana\_Hong](https://forum.katalon.com/u/Ana_Hong)\
**Post date:** [December 15, 2017, 5:09pm UTC](https://forum.katalon.com/t/stuggling-with-passing-variable-to-objects-xpath/8601/4 "2017-12-15T17:09:47Z")

</div>

Can you tell me what error message ? I suppose you should use either Integer or Number for that variable instead of String.

---

<div class="post-metadata">

**Author:** ![Gerlof\_Hoekstra](https://avatars.discourse-cdn.com/v4/letter/g/bb73d2/32.png) [@Gerlof\_Hoekstra](https://forum.katalon.com/u/Gerlof_Hoekstra)\
**Post date:** [December 15, 2017, 7:50pm UTC](https://forum.katalon.com/t/stuggling-with-passing-variable-to-objects-xpath/8601/5 "2017-12-15T19:50:48Z")

</div>

This is the error message:

Test Cases/CalculateTotalBooked FAILED because (of) Unable to click on object ‘Object Repository/Page\_AdactIn.com - Select Hotel (1)/td’ (Root cause: com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: ‘Object Repository/Page\_AdactIn.com - Select Hotel (1)/td’ located by ‘By.xpath: //td[count(. | id(“booked\_form”)/table[@class=“login”]/tbody[1]/tr[2]/td[1]/table[1]/tbody[1]/tr[RowNr]/td[1]) = count(id(“booked\_form”)/table[@class=“login”]/tbody[1]/tr[2]/td[1]/table[1]/tbody[1]/tr[RowNr]/td[1])][count(. | //_[@RowNr = ‘5’]) = count(//_[@RowNr = ‘5’])]’ not found)

I tried to use Number, but what do I have to fill in for Param in below table?  
Still confused about how this is supposed to work.  
 ![](https://s3.amazonaws.com/katalon-forum/editor/kz/uujgmx3o67a3.png "Image: https://s3.amazonaws.com/katalon-forum/editor/kz/uujgmx3o67a3.png")

[Capture4.PNG](https://s3.amazonaws.com/katalon-forum/editor/kz/uujgmx3o67a3.png)

---

<div class="post-metadata">

**Author:** ![Peter\_Wilson](https://avatars.discourse-cdn.com/v4/letter/p/9fc29f/32.png) [@Peter\_Wilson](https://forum.katalon.com/u/Peter_Wilson)\
**Post date:** [December 15, 2017, 8:17pm UTC](https://forum.katalon.com/t/stuggling-with-passing-variable-to-objects-xpath/8601/6 "2017-12-15T20:17:23Z")

</div>

I’ve been able to use a variable in the xpath with the following method.

I create an object with a path of:  
//table[@id=‘sales\_dash\_table’]/tbody/tr[${Variable}]/td

Then, within the code itself, I use the following:  
seasonalityPlanValue= WebUI.getText(findTestObject(‘Page\_/table\_object\_name’, [(‘Variable’) : index]))

The index is part of a loop, so it will be 1, 2, 3, etc as it iterates through.

---

<div class="post-metadata">

**Author:** ![Gerlof\_Hoekstra](https://avatars.discourse-cdn.com/v4/letter/g/bb73d2/32.png) [@Gerlof\_Hoekstra](https://forum.katalon.com/u/Gerlof_Hoekstra)\
**Post date:** [December 16, 2017, 6:28pm UTC](https://forum.katalon.com/t/stuggling-with-passing-variable-to-objects-xpath/8601/7 "2017-12-16T18:28:07Z")

</div>

YES, Peter, you made my day!  
[tr[${RowNr}]](http://forum.katalon.com/profile/id%3D%27sales_dash_table%27%5D%2Ftbody%2Ftr%5B%24%7BVariable%7D%5D%2Ftd "Link: http://forum.katalon.com/profile/id%3D%27sales\_dash\_table%27%5D%2Ftbody%2Ftr%5B%24%7BVariable%7D%5D%2Ftd") in the object’s xpath. does the trick.

Thank you!  
Gerlof

---

<div class="post-metadata">

**Author:** ![andy.gleed](https://avatars.discourse-cdn.com/v4/letter/a/4da419/32.png) [@andy.gleed](https://forum.katalon.com/u/andy.gleed)\
**Post date:** [May 5, 2020, 2:15pm UTC](https://forum.katalon.com/t/stuggling-with-passing-variable-to-objects-xpath/8601/8 "2020-05-05T14:15:38Z")

</div>

That also helped me, thank you. Turned out I was just missing the $ in my variable name in the object 🙂

---

<div class="post-metadata">

**Author:** ![Jaspreet.mangat](https://avatars.discourse-cdn.com/v4/letter/j/f475e1/32.png) [@Jaspreet.mangat](https://forum.katalon.com/u/Jaspreet.mangat)\
**Post date:** [July 7, 2020, 12:23pm UTC](https://forum.katalon.com/t/stuggling-with-passing-variable-to-objects-xpath/8601/9 "2020-07-07T12:23:07Z")

</div>

Hi , I am also trying to pass a variable in xpath but no success , The xpath is //\*[normalize-space(text())=${Qty}]

It works if i replace the variable Qty with value //\*[normalize-space(text())=6]

Please advice how can i pass the variable

---

<div class="post-metadata">

**Author:** ![mpokvytyte](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/mpokvytyte/32/18752_2.png) [@mpokvytyte](https://forum.katalon.com/u/mpokvytyte)\
**Post date:** [July 28, 2020, 2:00pm UTC](https://forum.katalon.com/t/stuggling-with-passing-variable-to-objects-xpath/8601/10 "2020-07-28T14:00:00Z")

</div>

I am having the same issue as Jaspreet, please can someone 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:** [July 28, 2020, 11:59pm UTC](https://forum.katalon.com/t/stuggling-with-passing-variable-to-objects-xpath/8601/11 "2020-07-28T23:59:09Z")

</div>

Does your test case script pass a parameter value appropriately?

```auto
TestObject tObj = findTestObject(String, ['Qty':6])

```

See

> **[Parameterize Web Test Objects](https://docs.katalon.com/katalon-studio/docs/parameterize-web-objects.html)**
>
> Parameterize Web Test Objects and their properties With parameterizing test objects, you can update test objects' locators dynamically by using either local or global variables. This feature comes in handy...

---

<div class="post-metadata">

**Author:** ![Jaspreet.mangat](https://avatars.discourse-cdn.com/v4/letter/j/f475e1/32.png) [@Jaspreet.mangat](https://forum.katalon.com/u/Jaspreet.mangat)\
**Post date:** [August 6, 2020, 1:31pm UTC](https://forum.katalon.com/t/stuggling-with-passing-variable-to-objects-xpath/8601/12 "2020-08-06T13:31:17Z")

</div>

@kazurayam ,No my test case doesn’t pass that parameter. what is the importance of that parameter (The one you mentioned in your script)

I am able to pass the global variable in the xpath as below  
//\*[contains(text(),’${GlobalVariable.Specification}’)]

But I donot need to use the global variable instead I want to use the local variable from the test.

---

<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:** [August 6, 2020, 9:46pm UTC](https://forum.katalon.com/t/stuggling-with-passing-variable-to-objects-xpath/8601/13 "2020-08-06T21:46:40Z")

</div>

> [@Jaspreet.mangat](#):
>
> what is the importance of that parameter (The one you mentioned in your script)

Invoking a method `findTestObject` is important. The method implements modifying a given String `//*[normalize-space(text())=${Qty}]` to a valid XPath expression `//*[normalize-space(text())=6]`.

Without calling the method `findTestObject`, a placeholder `${Qty}` will never be interpolated to `6`.

---

<div class="post-metadata">

**Author:** ![Jaspreet.mangat](https://avatars.discourse-cdn.com/v4/letter/j/f475e1/32.png) [@Jaspreet.mangat](https://forum.katalon.com/u/Jaspreet.mangat)\
**Post date:** [August 7, 2020, 9:32am UTC](https://forum.katalon.com/t/stuggling-with-passing-variable-to-objects-xpath/8601/14 "2020-08-07T09:32:19Z")

</div>

@kazurayam, Thanks . I did went through the [https://docs.katalon.com/katalon-studio/docs/parameterize-web-objects.html#example](https://docs.katalon.com/katalon-studio/docs/parameterize-web-objects.html#example) to understand it better .

Challenge that I am facing now is that If I use the global variable it working but I try to use a local variable it fails.

The Xpath is  
//_/text()[normalize-space(.)=‘${Specifications}’]/parent::_

In script view I am trying to get the text from this object as below :-

WebUI.getText(findTestObject(‘Products/Edit Specification/Added Specification Name - Copy’,  
[(‘Spec’) : Specifications]))

The screenshot from manual view is as below:-

 ![image](https://europe1.discourse-cdn.com/katalon/original/2X/4/47bf61ad3a1bb92c0b5cd4551e509d4177a1718a.png)

Specifications is a local variable declared as below in the test script  
mydate = new Date()  
formattedDate = mydate.format(‘MM/dd/yyyy HH mm ss’)  
Specifications = “$UpdatedSpecification$formattedDate”

My script fails saying  
Unable to find the element located by ‘By.xpath: //_/text()[normalize-space(.)=‘${Specifications}’]/parent::_’. Please recheck the objects properties to make sure the desired element is located.  
2020-08-07 11:31:40.472 INFO c.k.k.c.webui.common.WebUiCommonHelper - [SMART\_XPATH] Auto-trying out other XPath values in the test object.  
2020-08-07 11:31:40.472 INFO c.k.k.c.webui.common.WebUiCommonHelper - [SMART\_XPATH] No working XPath is found.

Please advice how can I use the local variables to pass the values to test objects.

---

<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:** [August 7, 2020, 12:29pm UTC](https://forum.katalon.com/t/stuggling-with-passing-variable-to-objects-xpath/8601/15 "2020-08-07T12:29:29Z")

</div>

```auto
// */text()[normalize-space(.)=’${Specifications}’]/parent::*

```

This string does not make any sence; not a valid XPath expression at all. No doubt you will get errors.

It is difficult for me to guess what you intended to express by this. Perhaps you need to study the XPath from scratch: See the following article.

- [https://www.guru99.com/xpath-selenium.html](https://www.guru99.com/xpath-selenium.html)

---

<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:** [August 7, 2020, 12:36pm UTC](https://forum.katalon.com/t/stuggling-with-passing-variable-to-objects-xpath/8601/16 "2020-08-07T12:36:37Z")

</div>

If your script has a line

```auto
WebUI.getText(findTestObject(‘...’, [‘Spec’ : '...']))

```

where you have a parameter named `Spec`, then the locator in the TestObject should be like this:

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

```

You should write the name of the parameter enclosed by `${ .... }`.

---

<div class="post-metadata">

**Author:** ![Jaspreet.mangat](https://avatars.discourse-cdn.com/v4/letter/j/f475e1/32.png) [@Jaspreet.mangat](https://forum.katalon.com/u/Jaspreet.mangat)\
**Post date:** [August 13, 2020, 8:31am UTC](https://forum.katalon.com/t/stuggling-with-passing-variable-to-objects-xpath/8601/17 "2020-08-13T08:31:04Z")

</div>

@kazurayam thanks, I will take a look at this.  
Actually I am stuck at Uploadfile functionality that need to be automated.

---

<div class="post-metadata">

**Author:** ![arthi.vigneshwari](https://avatars.discourse-cdn.com/v4/letter/a/e56c9b/32.png) [@arthi.vigneshwari](https://forum.katalon.com/u/arthi.vigneshwari)\
**Post date:** [October 2, 2020, 7:28pm UTC](https://forum.katalon.com/t/stuggling-with-passing-variable-to-objects-xpath/8601/18 "2020-10-02T19:28:18Z")

</div>

@kazurayam  
I am trying to solve a problem of clicking sort arrow in multiple column headers.  
Able to click in each column header by hardcoding variable value in script.  
How to pick the column name or variable value from the method parameter in the script to click all the column headers based on columnName passed in method parameter?

Tried:  
def sortAscendingOrDescending(String columnName){

```
	String expected = WebUI.getAttribute(findTestObject('CustomznObjects/BranchesListPage/ActivityHistoryTab/oCustomizationAH_ActivityHistoryTable_SortArrowAscend',['text':'Activity']),
	'class')

	WebUI.comment("Expected is" +expected)

```

}

Error message:  
Caused by: com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: ‘Object Repository/CustomznObjects/BranchesListPage/ActivityHistoryTab/oCustomizationAH\_ActivityHistoryTable\_SortArrowAscend’ located by ‘//th[contains(@id, ‘activitiesTable’) and contains(@title, ‘+columnName’)]/div[@class=“oj-table-column-header”]/div[@class=“oj-table-column-header-asc”]/a[contains(@class, ‘oj-table-column-header-asc’)]’ not found

Got this error message when tried to pass the local variable value as findTestObject(‘CustomznObjects/BranchesListPage/ActivityHistoryTab/oCustomizationAH\_ActivityHistoryTable\_SortArrowAscend’,[‘text’:’+columnName’]

Locator used:  
//th[contains(@id, ‘activitiesTable’) and contains(@title, ‘${text}’)]/div[@class=“oj-table-column-header”]/div[@class=“oj-table-column-header-asc”]/a[contains(@class, ‘oj-table-column-header-asc’)]

Please help.

---

<div class="post-metadata">

**Author:** ![grylion54](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/grylion54/32/16265_2.png) [@grylion54](https://forum.katalon.com/u/grylion54)\
**Post date:** [October 2, 2020, 8:46pm UTC](https://forum.katalon.com/t/stuggling-with-passing-variable-to-objects-xpath/8601/19 "2020-10-02T20:46:26Z")

</div>

@arthi.vigneshwari Maybe you are trying too soon. Have you tried to insert a wait statement, such as  
WebUI.waitForElementVisible(findTestObject(‘CustomznObjects/BranchesListPage/ActivityHistoryTab/oCustomizationAH\_ActivityHistoryTable\_SortArrowAscend’,[‘text’:‘Activity’])

before trying the getAttribute?

---

<div class="post-metadata">

**Author:** ![arthi.vigneshwari](https://avatars.discourse-cdn.com/v4/letter/a/e56c9b/32.png) [@arthi.vigneshwari](https://forum.katalon.com/u/arthi.vigneshwari)\
**Post date:** [October 6, 2020, 6:38am UTC](https://forum.katalon.com/t/stuggling-with-passing-variable-to-objects-xpath/8601/20 "2020-10-06T06:38:32Z")

</div>

@grylion54 Thanks for your reply. Exception is not due to wait issue.It is due to xpath not found. Am not sure how to pass the method parameter in findTestObject parameter value.Please help with it.

[Next page](https://forum.katalon.com/t/stuggling-with-passing-variable-to-objects-xpath/8601.md?page=2)
