Mobile Xpath contains(text(),'xx')

Hello,

In my Objet Repository when I use this Xpath :

//*[@class = ‘android.widget.TextView’ and (text() = ’ N°2015020397000007 ’ or . = ’ N°2015020397000007 ')]

Katalon find my object.

If I replace it by :

//*[@class = ‘android.widget.TextView’ and (contains(text(),‘N°2015020397000007’))]

it doesn’t find it anymore.

I don’t understand why ??

In fact I need to identify my object only with ‘N°’ so I wanted to use something like :

//*[@class = ‘android.widget.TextView’ and (contains(text(),‘N°’))]

Thank You !!


Other question :
My path for the object I’m looking for (detected by mobile Object Spy) is :

//hierarchy/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.ScrollView[1]/android.widget.LinearLayout[1]/android.widget.TextView[11]

How can I easily loop in all the last TextView ? without this absolute path ??
I need to check for specific “text” value for all the TextView[1] / TextView[2] / TextView[3] … / TextView[11] in bold above !

Thanks for your help

(I use Katalon Studio for a mobile application)

You should change text() to .. For more explanations, please read this post: https://stackoverflow.com/questions/3655549/xpath-containstext-some-string-doesnt-work-when-used-with-node-with-more

Other question :
My path for the object I’m looking for (detected by mobile Object Spy) is :

//hierarchy/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.ScrollView[1]/android.widget.LinearLayout[1]/ android.widget.TextView[11]

How can I easily loop in all the last TextView ? without this absolute path ??
I need to check for specific “text” value for all the TextView[1] / TextView[2] / TextView[3] … / TextView[11] in bold above !

You can use AppiumDriver.findElements() to find all elements that match the same locator.

import org.openqa.selenium.By
import org.openqa.selenium.WebElement

import com.kms.katalon.core.mobile.keyword.internal.MobileDriverFactory

import io.appium.java_client.AppiumDriver

AppiumDriver driver = MobileDriverFactory.getDriver()
List elements = driver.findElements(By.xpath("//hierarchy/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.ScrollView[1]/android.widget.LinearLayout[1]/android.widget.TextView"))

for (WebElement e in elements) {
	println e.getText()
}

findElements(By.xpath("…/android.widget.LinearLayout[1]/android.widget.TextView")) will return all TextView elements under the LinearLayout parent

Thank you for your help , it works!