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 无提示
滚动查找元素:
from appium.webdriver.common.mobileby import MobileBy
def 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()
本文详细介绍了UI自动化测试中几种常见的元素定位方法,包括id定位、accessibility_id定位、xpath定位以及uiautomator定位。特别是uiautomator定位,虽然其速度较快但书写复杂,且IDE无提示。此外,分享了一个使用uiautomator进行滚动查找元素的示例代码。对于难以唯一确定的元素,推荐使用xpath的组合定位策略。

4万+

被折叠的 条评论
为什么被折叠?



