Appium retrieves control instances based on xpath. appiumxpath

Source: Internet
Author: User
Tags appium

Appium retrieves control instances based on xpath. appiumxpath

As described in Appium's positioning methods for various FindElement controls based on Android, Appium has many methods for obtaining controls. One of them is to locate the control based on the XPATH of the control's page.

In this article, we will try to explain how Appium uses xpath to locate controls through our own experiments. If anything is wrong, please point it out.

1. background The experiment object used in this article is the NotePad application instance that comes with the SDK. Assume that there are already two note1 and note2 Notes added to Notepad, what we need to do is to use the xpath method to locate the TextView control under the ListView "note2. Note that there are three TextView controls on the interface:
  • The top part is the TextView with the Title Text of the entire ListView as "Notes", and the Index is 0.
  • The text in the middle is the TextView of "note2", and the Index is also 0
  • The bottom text is the TextView of "note1", and the Index is 1


2. xPath control acquisition method 2.1 All properties of the control can be used to determine the control properties, such as whether the control's text, index, resource-id is clickable, etc. For example: 2.1.1 search for the target control using text
    el = driver.findElementByXPath("//android.widget.TextView[contains(@text,'note2')]");        assertThat(el.getText(),equalTo("note2"));
2.1.2 search for the target control using index. If we use index 0 as the following method to search for the control note2, it will fail because the xpath only says "I want to search for android on the page. widget. the TextView-type index is 0 control, but as the background says, we actually have three TextView controls. The top and middle controls have 0 indexes. therefore, the final control is actually the TextView in which the top text found by Appium is "Notes.
    el = driver.findElementByXPath("//android.widget.TextView[contains(@index,0)]");        assertThat(el.getText(),equalTo("note2"));
Then we need to try to add a multi-point path so that xpath can identify the TextView whose index is 0, rather than the above. View the hierarchical structure of the UIAutomatorViewer control. The two textviews are split from LinearLayout, therefore, from this path, we should specify the TextView with the Index 0 under the ListView under the second FrameLayout in LinearLayout through the array subscript:
        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 search for the target control TBD by using other properties. These are not listed one by one. Refer to the above two examples for implementation.
2.2 According to my practice and understanding, it is unreliable to obtain the control by specifying the position of the set. There are at least two problems.
  • Problem 1: the position of the control in the set is uncontrollable.
        el = driver.findElementByXPath("//android.widget.TextView[1]");        assertThat(el.getText(),equalTo("note2"));
The above example is intended to find out the middle one of the top three textviews in UIAutomatorViewer, but the result is returned to the top "Notes ".
  • Problem 2: The specified Element Set cannot be searched by increasing the number of path layers.
        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"));
In the preceding example, I want to add the parent path to indicate that "the control I want is the first of the two TextView controls under ListView ", however, the returned message is "An unknown server-side error occurred while processing the command", which seems to be out of bounds. 2.4 xpath usage official manual for other non-mainstream ways to get control through xpath, please refer to the xpath official manual Chinese version: http://wenku.baidu.com/link? Url = tLdn9X4ucgmzYyKj4dpsgSDbshXEw6W8BTyqQAXd0kTMY6lBQqLiIL9FHtcZmdIeO8o5ZKV-fzlw96mFgvmWZ-qz35EPObyv8hKLTuyxbv3

Appium python xpath locating problems, new users

According to your idea, the LinearLayout In Front Of The ListView
It should be LinearLayout [1] \ ListView [0]. For the reason, think about the array call.

It is convenient to obtain xpath on the web page.
In chrome, right-click the element you want to obtain and review the element. In the developer window, right-click the code and select copy xpath.

Company restrictions, MOFA shows you the picture


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.