简体   繁体   中英

xPath for listing element in android (using appium automation)

I have a scrollable listing (search results) in Android App and for Appium autonation, I need to select a particular element from it. Appium inspector is providing information like index, resource-id and xpath.

content-desc:
type: android.widget.TextView
text: AMI
index: 0
enabled: true
location: {60, 513}
size: {81, 61}
checkable: false
checked: false
focusable: false
clickable: false
long-clickable: false
package: com.abc.xyz.debug
password: false
resource-id: com.abc.xyz.debug:id/student_label_text
scrollable: false
selected: false

xPath for first result (provided by appium inspector) is like this:

//android.support.v7.widget.RecyclerView[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.TextView[1]

for second result it is..

//android.support.v7.widget.RecyclerView[1]/android.widget.FrameLayout[2]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.TextView[1]

Above xPaths work but I am looking for shorter version. I tried various combinations but since I am new to this so not able to figure out the solution. Any help on this or any tools which can do this job?

Instead of XPath, you can use id right.

Using xpath will effect the speed of execution of our scripts.We can use id as shown below:

driver.findElement(By.id("student_label_text"))

XPath will take much time compared to id to identify the elements.

Best choice with Xpath usually is to find a unique value in any of the attributes of the element. From your example values, you could find by the text value:

driver.findElement(By.xpath("//android.widget.TextView[@text='AMI']"));

Since xpath is slower than other find strategies, you could first get all the elements that match with id and after that check what attribute values those elements have:

List<MobileElement> elements = driver.findElements(By.id("com.abc.xyz.debug:id/student_label_text"));
for (MobileElement element : elements) {
    if (element.getAttribute("text") == "AMI") {
        element.click();
    }
}

Note that I'm using findElements instead of findElement to get a List of all matching elements.

The usage of getAttribute command is not very well documented.. It took some digging to find out all the accepted names for attributes:

https://github.com/appium/appium-android-bootstrap/blob/master/bootstrap/src/io/appium/android/bootstrap/AndroidElement.java#L182

and

https://github.com/appium/appium-android-bootstrap/blob/master/bootstrap/src/io/appium/android/bootstrap/AndroidElement.java#L94

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM