And the Web side, like, APP end also can be positioned by the elements. There had practical experience in Web end seven elements positioning element positioning APP end is also easy to understand. Web positioning element end Click to view .
APP positioning element end, can uiautomator fetch page elements. Here are some of several elements targeting of APP:
1, id positioning
APP elements inside resource-id corresponds to the Web terminal element id.
find_element_by_id ( 'android: id / button2 ')
2, name positioning
Corresponds to the APP Web text elements inside end element name.
find_element_by_name ( 'Please enter your user name')
3, class positioning
APP class corresponding to elements inside the Web terminal element class.
find_element_by_class_name ( 'android.widget.EditText')
4, the relative positioning of
Is to find the relative positioning of the element has a parent element corresponding to the node attributes, then the positioning element based on the parent element.
Example: first through the resource-id = 'com.tal.kaoyan: id / activity_register_parentlayout' targeting LinearLayout ImageView parent element, and then by resource-id = 'com.tal.kaoyan: id / activity_register_userheader' targeting ImageView.
5, Xpath positioning
APP的Xpath定位方式和Web端的一毛一样。
find_element_by_xpath(’//android.widget.EditText[@text=“请输入用户名”]’)
6、list定位
List定位首先是使用find_elements_by_XX获取一组相同的class属性的元素,然后使用数组下标来区分标记不同元素进行相关操作。
images=driver.find_elements_by_id(‘com.tal.kaoyan:id/item_image’)
#选择第10张图作为上传对象
images[9].click()
7、UIAutomator元素定位
UIAutomator元素定位是 Android 系统原生支持的定位方式,虽然与 xpath 类似,但比它更加好用,且支持元素全部属性定位。定位原理是通过android自带的android uiautomator的类库去查找元素。 Appium元素定位方法其实也是基于Uiautomator来进行封装的。类似于Web端的CSS定位。
7.1、id定位
id定位是根据元素的resource-id属性来进行定位,使用 UiSelector().resourceId()方法即可。
find_element_by_android_uiautomator(‘new UiSelector().resourceId(“com.tal.kaoyan:id/login_password_edittext”)’)
7.2、text定位
text定位就是根据元素的text属性值来进行定位,new UiSelector()
find_element_by_android_uiautomator(‘new UiSelector().text(“请输入用户名”)’)
7.3, text positioning positioning class name
find_element_by_android_uiautomator(‘new UiSelector().className(“android.widget.EditText”)’)