# How to use variables in xpath (make dynamic xpath)

**URL:** <https://forum.katalon.com/t/how-to-use-variables-in-xpath-make-dynamic-xpath/24150>\
**Category:** Mobile Testing\
**Tags:** mobile-testing, katalon-studio\
**Created:** [April 30, 2019, 9:54am UTC](https://forum.katalon.com/t/how-to-use-variables-in-xpath-make-dynamic-xpath/24150 "2019-04-30T09:54:46Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![remix4you](https://avatars.discourse-cdn.com/v4/letter/r/6a8cbe/32.png) [@remix4you](https://forum.katalon.com/u/remix4you)\
**Post date:** [April 30, 2019, 9:54am UTC](https://forum.katalon.com/t/how-to-use-variables-in-xpath-make-dynamic-xpath/24150/1 "2019-04-30T09:54:46Z")

</div>

I have a code:

Date date = new Date()  
String datePart = date.format(‘dd MMMM yyyy’)

and xpath of object  
[//android.view.View](https://android.view.View)[@resource-id=‘android:id/month\_view’]/android.view.View[@content-desc=' **01 april 2019’**]

how to use variable datePart in this xpath to make dynamic xpath

---

<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:** [April 30, 2019, 10:14am UTC](https://forum.katalon.com/t/how-to-use-variables-in-xpath-make-dynamic-xpath/24150/2 "2019-04-30T10:14:24Z")

</div>

You can create your TestObject dynamically in the code (it won’t be persisted in Object Repository)

```
TestObject to = new TestObject()
to.addProperty('xpath', ConditionType.EQUALS, "//android.view.View](https://android.view.view/)[@resource-id=‘android:id/month_view’]/android.view.View[@content-desc='" + datePart + "'")
WebUI.click(to) // or any other keyword
```

---

<div class="post-metadata">

**Author:** ![remix4you](https://avatars.discourse-cdn.com/v4/letter/r/6a8cbe/32.png) [@remix4you](https://forum.katalon.com/u/remix4you)\
**Post date:** [May 3, 2019, 12:55pm UTC](https://forum.katalon.com/t/how-to-use-variables-in-xpath-make-dynamic-xpath/24150/3 "2019-05-03T12:55:17Z")

</div>

# where is the mistake ??? when i use TestObject testObjectOne = new TestObject() testObjectTwo.addProperty(“xpath”, ConditionType.EQUALS, “[//android.view.View](https://android.view.View)[@resource-id=‘android:id/month\_view’]/android.view.View[@content-desc=‘03 may 2019’]”) Mobile.tap(testObjectOne, 2) Everything works correctly - the date is selected

when i try to use dynamic path  
TestObject testObjectOne = new TestObject()  
testObjectOne.addProperty(“xpath”, ConditionType.EQUALS, ‘[//android.view.View](https://android.view.View)[@resource-id=‘android:id/month\_view’]/android.view.View["’ + datePart + ‘"]’)  
Mobile.tap(testObjectOne, 2)  
NO ERRORS but the date is NOT selected.

---

<div class="post-metadata">

**Author:** ![HeleneB](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/heleneb/32/2775_2.png) [@HeleneB](https://forum.katalon.com/u/HeleneB)\
**Post date:** [May 3, 2019, 1:07pm UTC](https://forum.katalon.com/t/how-to-use-variables-in-xpath-make-dynamic-xpath/24150/4 "2019-05-03T13:07:59Z")

</div>

> [@remix4you](#):
>
> `/android.view.View["’ + datePart + ‘"]’)`

> [@Marek\_Melocik](#):
>
> ```auto
> /android.view.View[@content-desc='" + datePart + "']")
> 
> ```

You forgot `@content-desc=` and the quote symbols are inverted

---

<div class="post-metadata">

**Author:** ![remix4you](https://avatars.discourse-cdn.com/v4/letter/r/6a8cbe/32.png) [@remix4you](https://forum.katalon.com/u/remix4you)\
**Post date:** [May 3, 2019, 1:18pm UTC](https://forum.katalon.com/t/how-to-use-variables-in-xpath-make-dynamic-xpath/24150/5 "2019-05-03T13:18:45Z")

</div>

Thanks problem solved  
TestObject to = new TestObject()  
to.addProperty(‘xpath’, ConditionType.EQUALS, (’[//android.view.View](https://android.view.View)[@resource-id=‘android:id/month\_view’]/android.view.View[@content-desc=’’ +  
datePart) + ‘’]’)  
Mobile.tap(to, 2)

---

<div class="post-metadata">

**Author:** ![branyernunez](https://avatars.discourse-cdn.com/v4/letter/b/41988e/32.png) [@branyernunez](https://forum.katalon.com/u/branyernunez)\
**Post date:** [March 15, 2020, 10:35pm UTC](https://forum.katalon.com/t/how-to-use-variables-in-xpath-make-dynamic-xpath/24150/6 "2020-03-15T22:35:39Z")

</div>

Estoy intentando hacerle click a un objecto en un List View donde hay varios objecto he intentado hacerlo de varias maneras y no me sale

TestObject obj = new TestObject();  
obj.addProperty(‘xpath’, ConditionType.EQUALS, “//hierarchy/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.webkit.WebView[1]/android.webkit.WebView[1]/android.view.View[1]/android.view.View[2]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[2]/android.view.View[1]/android.view.View[6]/android.view.View[2]/android.view.View[1]/android.widget.ListView[1]/android.view.View[1]/android.view.View[2]/android.view.View[1]/android.view.View[@content-desc=’”+" - " +cuenta+"’]",true)

```
	try {
		for (int i = 0; i <= intento; i++) {
			MobileDriverFactory elemento = MobileDriverFactory.driver.findElementByXPath(obj)
			if (elemento != null) {
				Mobile.tap(obj, 10)
				GlobalVariable.variableBooleanTemp = true
				break
			} else {
				Mobile.swipe(300, 900, 300, 200)
				Mobile.delay(3)
			}

```

He probado con esto y no me sale por favor si alguien le ha dado el mismo error y ha encontrado alguna solucion @Chris_Trevarthen

---

<div class="post-metadata">

**Author:** ![Chris\_Trevarthen](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/chris_trevarthen/32/358_2.png) [@Chris\_Trevarthen](https://forum.katalon.com/u/Chris_Trevarthen)\
**Post date:** [March 17, 2020, 3:02am UTC](https://forum.katalon.com/t/how-to-use-variables-in-xpath-make-dynamic-xpath/24150/7 "2020-03-17T03:02:43Z")

</div>

Hi @branyernunez,

The xpath you’re using seems very long, so your test could break easily if there are small changes. I would recommend trying to give the views within your ListView a `resource-id`, if you can (this would require a change from a developer with access to your app’s code). This way, you could narrow down the xpath to a much shorter value, such as:

`//android.view.View[@resource-id="my-list-of-items" and @content-desc="'" + " - " + cuenta + "'"]`

If you can’t update the code, you could try making the xpath shorter anyway using just the last part of what you have:

` //android.widget.ListView//android.view.View[@content-desc="'" + " - " + cuenta + "'"]`

Finally, if what you’re trying to do is scroll through a list of elements, you can try an open source library that I created to help with common functions: [https://github.com/detroit-labs/katalon-mobile-util](https://github.com/detroit-labs/katalon-mobile-util)

For scrolling functionality: [https://github.com/detroit-labs/katalon-mobile-util#scrolling](https://github.com/detroit-labs/katalon-mobile-util#scrolling)

Hope this helps,

Chris

---

<div class="post-metadata">

**Author:** ![branyernunez](https://avatars.discourse-cdn.com/v4/letter/b/41988e/32.png) [@branyernunez](https://forum.katalon.com/u/branyernunez)\
**Post date:** [March 17, 2020, 3:42am UTC](https://forum.katalon.com/t/how-to-use-variables-in-xpath-make-dynamic-xpath/24150/8 "2020-03-17T03:42:26Z")

</div>

hola @Chris_Trevarthen lo hice de la manera en que me dices y no me funciona [//android.widget.ListView//android.view.View](https://android.widget.ListView//android.view.View)[@content-desc="’" + " - " + cuenta + “’”], lo hago asi y no me funciona, podrias darme una ayuda para intentara solucionar eso, tengo varias semanas intentando y no he podido, cualquier cosa este es mi correo [branyernuez@gmail.com](mailto:branyernuez@gmail.com)

---

<div class="post-metadata">

**Author:** ![Chris\_Trevarthen](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/chris_trevarthen/32/358_2.png) [@Chris\_Trevarthen](https://forum.katalon.com/u/Chris_Trevarthen)\
**Post date:** [March 17, 2020, 3:57am UTC](https://forum.katalon.com/t/how-to-use-variables-in-xpath-make-dynamic-xpath/24150/9 "2020-03-17T03:57:36Z")

</div>

Hi @branyernunez,

Do you get an error in the Katalon logs? Could you post it here?

Can you also try the steps in this post to see what is on the screen at the time of the test (do this part right before you start your `try` statement from above):

> [@Unable to click/tap on a button in Android WebView](http://forum.katalon.com/t/unable-to-click-tap-on-a-button-in-android-webview/20135/7):
>
> Hi @Sravya_Batchu, That’s a lot of good information, thank you! I’m curious what Katalon and Appium think are on the screen at the time you switch to the WebView. If you want to see some more detail about exactly what Appium/Katalon detects on the screen you can log out the XML contents of the screen. You should be able to do that by putting the following import statements at the top of your test case: import com.kms.katalon.core.logging.KeywordLogger import com.kms.katalon.core.mobile.keywor…

– Chris
