How do I get elements in Appium

In automation, element positioning is the first difficulty we encountered. There will always be various problems that result in our being unable to find items. The previous blog also wrote about various methods of element positioning. Today I am mainly going to share xpath's positioning method.

Here we take the calculator as an example.

For example, I want to find button number 1 here.

First open the UIuiautomatorviewer

 

We can see the location of this button by clicking the number 7 with the mouse. As mentioned in the previous article, in the element's attributes, class is the address of xpath, and we can write xpath through class.

Then we can write here:

find_element_by_xpath ("// android.support.v4.view.ViewPager / android.widget.LinearLayout / android.widget.LinearLayout [3] /android.widget.Button [1]")
When you look here, it feels like peeling an onion. Peel layer by layer from the outside in and finally find the target we want to locate.
It should be noted here that we cannot start from the entire outermost side when finding it in xpath, which will lead to an error. What is the top-level element that the control contains to start writing from the top-level class that contains the control? For example the android.support.v4.view.ViewPager here, we can click on it to find
android.support.v4.view.ViewPager It's actually the entire keyboard area. Then reposition the number key 1 in the keyboard area.
And what I wrote above is android.widget.LinearLayout [3] /android.widget.Button [1] instead of android.widget.LinearLayout [2] /android.widget.Button [0] Looks like usUIuiautomatorviewerThe information received is not the same. Why is that? The author doesn't actually explain it clearly haha. I just think android.widget.LinearLayout [3] /android.widget.Button [1] is the third one to locate android.widget.LinearLayoutThe first one is inandroid.widget.ButtonElement. I remember in the book I saw that element positioning in xpath starts at 1 instead of 0, so I passed android.widget.LinearLayout [3] /android.widget.Button [1] go to control by number 1. What is being said here may be wrong. I am also looking for the answer and will update it when I find the answer.

 

Reprinted at: https://www.cnblogs.com/rookie-c/p/5886776.html