# Dynamic Xpath and Passing Variable Into the Xpath

**URL:** <https://forum.katalon.com/t/dynamic-xpath-and-passing-variable-into-the-xpath/29684>\
**Category:** Web Testing\
**Tags:** katalon-studio\
**Created:** [July 16, 2019, 6:27am UTC](https://forum.katalon.com/t/dynamic-xpath-and-passing-variable-into-the-xpath/29684 "2019-07-16T06:27:37Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![marias](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@marias](https://forum.katalon.com/u/marias)\
**Post date:** [July 16, 2019, 6:27am UTC](https://forum.katalon.com/t/dynamic-xpath-and-passing-variable-into-the-xpath/29684/1 "2019-07-16T06:27:37Z")

</div>

Hi everyone,

I was wondering if someone could help me out with how to make a dynamic Xpath. I need to click on a button that has an xpath that changes depending on the “case number” The following is the xpath:

//\*[@id=“checkCases\_No\_4499”]/td[9]/div[1]/div/div/button/span

I tried to use the following function:

new\_btn= WebUI.modifyObjectProperty(findTestObject(‘Object Repository/Page\_Designer Dashboard/span\_Assign to me’), ‘xpath’, ‘equals’, ‘//tr[@id=“checkCases\_No\_4500”]/td[9]/div[1]/div/div/button/span’, false)

However, with this I still need to manually change the case number every time I run it.

How do I store the case number as a variable and pass it in the xpath?

Thanks so much! 🙂

---

<div class="post-metadata">

**Author:** ![Marek\_Melocik](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/marek_melocik/32/371_2.png) [@Marek\_Melocik](https://forum.katalon.com/u/Marek_Melocik)\
**Post date:** [July 16, 2019, 8:13am UTC](https://forum.katalon.com/t/dynamic-xpath-and-passing-variable-into-the-xpath/29684/2 "2019-07-16T08:13:07Z")

</div>

Hello,

where does ‘case number’ come from?

---

<div class="post-metadata">

**Author:** ![marias](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@marias](https://forum.katalon.com/u/marias)\
**Post date:** [July 16, 2019, 8:17am UTC](https://forum.katalon.com/t/dynamic-xpath-and-passing-variable-into-the-xpath/29684/3 "2019-07-16T08:17:42Z")

</div>

It gets automatically generated once a case is sent to our site. It appears on the left side of the screen

 ![Case_number](https://europe1.discourse-cdn.com/katalon/original/2X/1/1d792ab7b307023b68876bc96ce5457009377ba7.png)

The button that I want to click is on the right side. This button has an xpath that is dependent on the case number.

---

<div class="post-metadata">

**Author:** ![Marek\_Melocik](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/marek_melocik/32/371_2.png) [@Marek\_Melocik](https://forum.katalon.com/u/Marek_Melocik)\
**Post date:** [July 16, 2019, 8:29am UTC](https://forum.katalon.com/t/dynamic-xpath-and-passing-variable-into-the-xpath/29684/4 "2019-07-16T08:29:24Z")

</div>

What about this?

`String caseNumber = WebUI.getText(findTestObject('Object/With/CaseNumber'))`

And then

`new_btn= WebUI.modifyObjectProperty(findTestObject(‘Object Repository/Page_Designer Dashboard/span_Assign to me’), ‘xpath’, ‘equals’, ‘//tr[@id=“checkCases_No_' + caseNumber + '”]/td[9]/div[1]/div/div/button/span’, false)`

---

<div class="post-metadata">

**Author:** ![marias](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@marias](https://forum.katalon.com/u/marias)\
**Post date:** [July 16, 2019, 8:57am UTC](https://forum.katalon.com/t/dynamic-xpath-and-passing-variable-into-the-xpath/29684/5 "2019-07-16T08:57:27Z")

</div>

I tried doing that but once I do WebUI.click(new\_btn) the test fails.

Also, the ‘Object/With/CaseNumber’ also has an xpath that depends on that case number itself.

I tried manually putting String caseNumber = ‘4499’ and passing it to through the modifyObjectProperty function. While it says that I was successfully able to change the property, the test still fails when I click on the button.

Here is the error message:

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

Thanks so much for the help!

---

<div class="post-metadata">

**Author:** ![Marek\_Melocik](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/marek_melocik/32/371_2.png) [@Marek\_Melocik](https://forum.katalon.com/u/Marek_Melocik)\
**Post date:** [July 16, 2019, 9:37am UTC](https://forum.katalon.com/t/dynamic-xpath-and-passing-variable-into-the-xpath/29684/6 "2019-07-16T09:37:52Z")

</div>

Can you share HTML code of the table row with case number? Also, full exception would be helpful.

---

<div class="post-metadata">

**Author:** ![marias](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@marias](https://forum.katalon.com/u/marias)\
**Post date:** [July 17, 2019, 12:34am UTC](https://forum.katalon.com/t/dynamic-xpath-and-passing-variable-into-the-xpath/29684/7 "2019-07-17T00:34:41Z")

</div>

Hi @Marek_Melocik,

Is this what you were asking?

4492

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

 ![image](https://europe1.discourse-cdn.com/katalon/original/2X/9/989509d2a56754a73821a43ceb9f4e1b0fcaa93d.png)

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

Thanks so much!

---

<div class="post-metadata">

**Author:** ![marias](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@marias](https://forum.katalon.com/u/marias)\
**Post date:** [July 17, 2019, 12:46am UTC](https://forum.katalon.com/t/dynamic-xpath-and-passing-variable-into-the-xpath/29684/8 "2019-07-17T00:46:48Z")

</div>

![image](https://europe1.discourse-cdn.com/katalon/original/2X/9/99eeb19e2828cb4bb591200a64b9e5b99e83e7fa.png)

And this? Is this what you were asking?  
Thanks!

---

<div class="post-metadata">

**Author:** ![Marek\_Melocik](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/marek_melocik/32/371_2.png) [@Marek\_Melocik](https://forum.katalon.com/u/Marek_Melocik)\
**Post date:** [July 17, 2019, 5:16am UTC](https://forum.katalon.com/t/dynamic-xpath-and-passing-variable-into-the-xpath/29684/9 "2019-07-17T05:16:30Z")

</div>

Exception says element not interactable - is it displayed correctly at the time you test it? And I may need more HTML code, to make sure your xpath is correct.

---

<div class="post-metadata">

**Author:** ![marias](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@marias](https://forum.katalon.com/u/marias)\
**Post date:** [July 18, 2019, 1:16am UTC](https://forum.katalon.com/t/dynamic-xpath-and-passing-variable-into-the-xpath/29684/10 "2019-07-18T01:16:07Z")

</div>

What does element not interactable mean? Thanks!

---

<div class="post-metadata">

**Author:** ![marias](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@marias](https://forum.katalon.com/u/marias)\
**Post date:** [July 18, 2019, 2:35am UTC](https://forum.katalon.com/t/dynamic-xpath-and-passing-variable-into-the-xpath/29684/11 "2019-07-18T02:35:07Z")

</div>

Hi @Marek_Melocik

This is the xpath that Katalon was able to detect using spyweb. When I run it with this xpath (without modifying anything), it works.

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

I modified the xpath using this:  
String xpath\_assign = ‘//tr[@id="checkCases\_No\_’ + caseNumber + ‘"]/td[9]/div[1]/div/div/button’

TestObject AssignButton = new TestObject(‘AssignButton’).addProperty(“xpath\_assign”, ConditionType.EQUALS, xpath\_assign)

WebUI.delay(5)

WebUI.click(AssignButton)

While it says that I am able to change it properly, the xpath generated is:

![image](https://europe1.discourse-cdn.com/katalon/original/2X/3/38cdbc4035362c7714d75fe5a2d7439d3094f924.png)

It has double quotation marks around the ID but I do not know how to make it single quotation marks. Could that be why?

---

<div class="post-metadata">

**Author:** ![Marek\_Melocik](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/marek_melocik/32/371_2.png) [@Marek\_Melocik](https://forum.katalon.com/u/Marek_Melocik)\
**Post date:** [July 18, 2019, 5:19am UTC](https://forum.katalon.com/t/dynamic-xpath-and-passing-variable-into-the-xpath/29684/12 "2019-07-18T05:19:50Z")

</div>

> [@marias](#):
>
> TestObject AssignButton = new TestObject(‘AssignButton’).addProperty(“xpath\_assign”, ConditionType.EQUALS, xpath\_assign)

↑ This is not correct (property name). ↑

`TestObject AssignButton = new TestObject(‘AssignButton’).addProperty(“xpath”, ConditionType.EQUALS, xpath_assign)`

And no, it should be the same if you use single or double quotes.

---

<div class="post-metadata">

**Author:** ![marias](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@marias](https://forum.katalon.com/u/marias)\
**Post date:** [July 18, 2019, 5:26am UTC](https://forum.katalon.com/t/dynamic-xpath-and-passing-variable-into-the-xpath/29684/13 "2019-07-18T05:26:15Z")

</div>

Yeah it works - thanks so much 🙂

---

<div class="post-metadata">

**Author:** ![aswathy.sreejith](https://avatars.discourse-cdn.com/v4/letter/a/e8c25b/32.png) [@aswathy.sreejith](https://forum.katalon.com/u/aswathy.sreejith)\
**Post date:** [December 2, 2019, 9:00am UTC](https://forum.katalon.com/t/dynamic-xpath-and-passing-variable-into-the-xpath/29684/14 "2019-12-02T09:00:12Z")

</div>

please help me on query,

my x-path is : WebUI.click(findTestObject(‘Object Repository/Admin/AcceptButton’))

Object repo, AcceptButton : //\*[contains(text(),‘krish’)]/following::div[1]/button[text()=‘Accept’]

I am storing ‘krish’ in varaible: ProgramName,

So that am giving this variable name in xpath like this,

//\*[contains(text(),’+ProgramName+’)]/following::div[1]/button[text()=‘Accept’]

but its not clicking, pls help me on this,

Advance Thanks,  
😄

---

<div class="post-metadata">

**Author:** ![Marek\_Melocik](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/marek_melocik/32/371_2.png) [@Marek\_Melocik](https://forum.katalon.com/u/Marek_Melocik)\
**Post date:** [December 2, 2019, 9:11am UTC](https://forum.katalon.com/t/dynamic-xpath-and-passing-variable-into-the-xpath/29684/15 "2019-12-02T09:11:58Z")

</div>

Hello,

this article may help you

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

---

<div class="post-metadata">

**Author:** ![aswathy.sreejith](https://avatars.discourse-cdn.com/v4/letter/a/e8c25b/32.png) [@aswathy.sreejith](https://forum.katalon.com/u/aswathy.sreejith)\
**Post date:** [December 2, 2019, 9:40am UTC](https://forum.katalon.com/t/dynamic-xpath-and-passing-variable-into-the-xpath/29684/16 "2019-12-02T09:40:49Z")

</div>

Thanks for quick response,

Its working as i expected

---

<div class="post-metadata">

**Author:** ![kristine\_paula](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kristine_paula/32/22830_2.png) [@kristine\_paula](https://forum.katalon.com/u/kristine_paula)\
**Post date:** [September 8, 2020, 1:32pm UTC](https://forum.katalon.com/t/dynamic-xpath-and-passing-variable-into-the-xpath/29684/17 "2020-09-08T13:32:45Z")

</div>

hi, please help me

//button[@data-pika-day = 'startDay' and @data-pika-month = 'startMonth']

im trying to put the variable in xpath but its not working

Caused by: com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: ‘Object Repository/a\_Universal/start’ located by ‘By.xpath: //button[@data-pika-day = 'startDay' and @data-pika-month = 'startMonth']’ not found

 ![image](https://europe1.discourse-cdn.com/katalon/original/2X/0/024bed938d5b1053bb6c583d5d1bcbbb1c109a82.png)

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

---

<div class="post-metadata">

**Author:** ![salman.ahmad](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/salman.ahmad/32/17516_2.png) [@salman.ahmad](https://forum.katalon.com/u/salman.ahmad)\
**Post date:** [August 11, 2021, 7:46am UTC](https://forum.katalon.com/t/dynamic-xpath-and-passing-variable-into-the-xpath/29684/18 "2021-08-11T07:46:02Z")

</div>

This worked for me thank you sooooo much Marek
