How to use variables in xpath (make dynamic xpath)

I have a code:

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

and xpath of object
//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

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

where is the mistake ???
when i use
TestObject testObjectOne = new TestObject()
testObjectTwo.addProperty(“xpath”, ConditionType.EQUALS, “//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[@resource-id=‘android:id/month_view’]/android.view.View["’ + datePart + ‘"]’)
Mobile.tap(testObjectOne, 2)
NO ERRORS but the date is NOT selected.

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

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

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

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

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

Hope this helps,

Chris

hola @Chris_Trevarthen lo hice de la manera en que me dices y no me funciona //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

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):

– Chris