03-05 APP自动化测试-常用定位方式

id 定位

dirver.find_element_by_id(resource-id 属性值) driver.find_element_by(MobileBy.ID, resource-id 属性值)

accessibility_id 定位
driver.find_element_by_accessibility_id(content-desc 属性值) driver.find_element_by(MobileBy.ACCESSIBILITY_ID, content-desc 属性值)

xpath 定位
driver.find_element_by_xpath(xpath 属性值)

当遇到无法唯一定位的情况,建议使用 xpath 的组合定位
uiautomator 定位
优点:
  • uiautomator 是 Android 的工作引擎,比 xpath 定位速度快
缺点:
  • 表达式书写复杂,容易写错,IDE 无提示
Uiautomator--Uiselector元素定位
【03-05 APP自动化测试-常用定位方式】滚动查找元素:
from appium.webdriver.common.mobileby import MobileBydef find_by_scroll(self): self.driver.find_element( MobileBy.ANDROID_UIAUTOMATOR, 'new UiScrollable(new UiSelector().' 'scrollable(true).instance(0)).' 'scrollIntoView(new UiSelector().' 'text("想要查到的文本").'# 修改想要查找的文本信息即可 'instance(0)); ').click()

    推荐阅读