appium获取元素的方式和总结

一、非uiautomator
1.resource-id:
driver.find_element_by_id(id)
2.class:
driver.find_element_by_class(class)
3.text(实际上是xpath):
xpath = “//android.widget.TextView[@text=‘中国’]”
element = driver.find_element_by_xpath(xpath)
注:android.widget.TextView为class
4.xpath:
xpath = “//android.widget.TextView[@text=‘登录’]”
element = driver.find_element_by_xpath(xpath)
5.content-desc:
driver.find_element_by_android_uiautomator(‘new UiSelector().description(“立即注册”)’)
二、uiautomator
1.resourceId:
driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("%s")’)
2.text:
driver.find_element_by_android_uiautomator(‘new UiSelector().text("%s")’)
3.description:
driver.find_element_by_android_uiautomator(‘new UiSelector().description("%s")’)
4.className:
driver.find_element_by_android_uiautomator(‘new UiSelector().className("%s")’)
5.index:
driver.find_element_by_android_uiautomator(‘new UiSelector().index("%s")’)
6.className + index
driver.find_element_by_android_uiautomator(
‘new UiSelector().className("%s").childSelector(new UiSelector().index("%d"))’)
7.伪xpath:
#通过同级元素定位同级元素
driver.find_element_by_android_uiautomator
(‘new UiSelector().text(“Custom View”).fromParent(new UiSelector().text(“Accessibility Service”))’)
.click()
#通过父级元素定位子集元素
driver.find_element_by_android_uiautomator
(‘new UiSelector().className(“android.widget.ListView”).childSelector(new UiSelector().text(“Custom View”))’)
.click()
三、总结获取元素
way_list = {“id”: “resourceId”, “class_name”: “className”, “text”: “text”, “contentDescription”: “description”}
def find_element_by_way(self, element, way):
log.logger.info(u"通过%s方式查找元素%s" % (way, element))
way1 = way.replace(" “, “”)
# 遍历出way_list字典里的所有内容
for key, value in way_list.items():
if way1 == key:
ele = self.driver.find_element_by_android_uiautomator(
‘new UiSelector().’ + value + '(”’ + element + ‘")’)
return ele

参考原文链接:https://www.jianshu.com/p/5d19931058c3

你可能感兴趣的:(appium获取元素的方式和总结)