Appium--XPath positioning elements

Source: Internet
Author: User
Tags appium

Appium has a number of ways to get controls, as described in the article, "Appium-based control localization practices for various findelement on Android." One is to locate the control based on the XPath of the page on which the control resides.

This article attempts to experiment with the Appium how to use XPath to locate the control, if there is a wrong place, please point out.

1. Background

The experiment object that this article tries to use is the SDK comes with the Notepad application instance, suppose already has two notes namely "Note1" and "Note2" to add to Notepad above, what we want to do is try to use XPath method to locate "Note2" This listview below the TextView control.

Note that there are 3 textview types of controls on the interface: top of the Textview,index as the entire ListView with the title text "Notes" for the middle of the text as "Note2" Textview,index is also 0 the bottom text is " Note1 "of the TextView.

e1 = Driver.findelementbyxpath ("//android.widget.textview[contains (@text, ' Note2 ')]");

Assertthat (El.gettext (), Equalto ("Note2"));

Again as below:

This element does not have a Text property on the page and has no cotent-des properties, so there is no way to correctly locate it.

Correct positioning method: X.find_element ("XPath", "//android.widget.relativelayout/android.widget.textview[1]")

Index starting from 0, positioning the second with textview[1]

2.1.2 to find the target control by index

If we use index to find the control Note2 as in the following way, it will fail because the XPath only says "I'm looking for a control of the Android.widget.TextView type index 0 on the page", But as the background says, we actually have 3 TextView controls,

One of the top and middle controls their index is 0. So the final control is actually the Appium first to find the top text for "Notes" TextView.

    el = Driver.findelementbyxpath ("//android.widget.textview[contains (@index, 0)]");        Assertthat (El.gettext (), Equalto ("Note2"));

Then we'll have to figure out a way to add more paths, so that XPath can tell what is needed is the TextView of index 0 below, not the one above. Watch the hierarchical structure of the Uiautomatorviewer control and find that the two TextView are forked from the LinearLayout ,

so we should start with this path by assigning the array subscript What we need is "the TextView under the second framelayoutbelow the linearlayout below the listview below the index 0:

        el = Driver.findelementbyxpath ("//android.widget.linearlayout[1]/android.widget.framelayout/ Android.widget.listview/android.widget.textview[contains (@index, 0)] ");        Assertthat (El.gettext (), Equalto ("Note2"));
2.1.3 to find the target control through other properties

TBD, these will not be listed, refer to the above two examples to implement it.

2.2 Get control by specifying the location of the selection collection

According to my practice and understanding, it is not reliable to get the control in this way, at least there are two problems. Issue 1: The control's position in the collection is not controllable.

        el = Driver.findelementbyxpath ("//android.widget.textview[1]");        Assertthat (El.gettext (), Equalto ("Note2"));

In the above example, I would like to find out the middle of the upper and lower 3 textview in the Uiautomatorviewer, but the result is to return to my really top "Notes". Issue 2: Finding a collection of specified elements is not effective by increasing the path layer limit.

        el = Driver.findelementbyxpath ("//android.widget.framelayout[1]/android.widget.linearlayout[1]/ Android.widget.framelayout/android.widget.listview/android.widget.textview[0] ");        Assertthat (El.gettext (), Equalto ("Note2"));

The above example is intended to indicate that the "I want the control is the first of the two TextView controls under the ListView" by increasing the limit of the parent path, but returned to me is "an unknown server-side error occurred while Processing the command ", the feeling is crossed out of the look.

2.4 Official Handbook of XPath usage

For other non-mainstream methods of using XPath to take control, please refer to the official XPath manual in Chinese version: http://wenku.baidu.com/link?url= Tldn9x4ucgmzyykj4dpsgsdbshxew6w8btyqqaxd0ktmy6lbqqliil9fhtczmdieo8o5zkv-fzlw96mfgvmwz-qz35epobyv8hkltuyxbv3

Appium--XPath positioning elements

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.