Appium UIAutomator定位简介

UIAutomator定位简介
UIAutomator元素定位是 Android 系统原生支持的定位方式,虽然与 xpath 类似,但比它更加好用,且支持元素全部属性定位.定位原理是通过android 自带的android uiautomator的类库去查找元素。 Appium元素定位方法其实也是基于Uiautomator来进行封装的。
使用方法 find_element_by_android_uiautomator() 可以运用UiAutomator元素定位。
定位方法
id定位
text定位
class name定位
id定位
id定位是根据元素的resource-id属性来进行定位,使用 UiSelector().resourceId()方法即可。
by_Uiautomator_id.py

from find_element.capability import driver

driver.find_element_by_android_uiautomator\
    ('new UiSelector().resourceId("com.tal.kaoyan:id/login_email_edittext")').send_keys('coco123123')#用户名
driver.find_element_by_android_uiautomator\
    ('new UiSelector().resourceId("com.tal.kaoyan:id/login_password_edittext")').send_keys('coco123123')#密码
driver.find_element_by_android_uiautomator\
    ('new UiSelector().resourceId("com.tal.kaoyan:id/login_login_btn")').click()#登录

text定位
text定位就是根据元素的text属性值来进行定位,new UiSelector()

driver.find_element_by_android_uiautomator\
    ('new UiSelector().text("请输入用户名")').send_keys('coco123123')

by_Uiautomator_text.py

from find_element.capability import driver

driver.find_element_by_android_uiautomator\
    ('new UiSelector().text("请输入用户名")').send_keys('coco123123')
driver.find_element_by_android_uiautomator\
    ('new UiSelector().resourceId("com.tal.kaoyan:id/login_password_edittext")').send_keys('coco123123')#密码
driver.find_element_by_android_uiautomator\
    ('new UiSelector().resourceId("com.tal.kaoyan:id/login_login_btn")').click()#登录

class name定位
与Appium class定位方式一样,也是根据元素的class属性来进行定位。

driver.find_element_by_android_uiautomator\
    ('new UiSelector().className("android.widget.EditText")').send_keys('coco123123')

by_Uiautomator_classname.py

from find_element.capability import driver

driver.find_element_by_android_uiautomator\
    ('new UiSelector().className("android.widget.EditText")').send_keys('coco123123')
driver.find_element_by_android_uiautomator\
    ('new UiSelector().resourceId("com.tal.kaoyan:id/login_password_edittext")').send_keys('coco123123')#密码
driver.find_element_by_android_uiautomator\
    ('new UiSelector().resourceId("com.tal.kaoyan:id/login_login_btn")').click()#登录

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43264177/article/details/82952073