二、appium+python xpath、android_uiautomator定位元素的方法
1.继承webdriver的几个常见的定位方法
a.find_element_by_id("id")
b.find_element_by_name() #text值,appium 1.14.0+python 3.7这个方法提示不支持
c.find_element_by_className()
2.appium拓展方法
这里以微信会话界面为例
a.find_element_by_android_uiautomator('new UiSelector().text("")')
# device_a.find_element_by_android_uiautomator('new UiSelector().text("CYDtest594")').click() # device_a.find_element_by_android_uiautomator('new UiSelector().textContains("CYD")').click() # device_a.find_element_by_android_uiautomator('new UiSelector().textStartsWith("CYD")').click() device_a.find_element_by_android_uiautomator('new UiSelector().textMatches("CYD(.*)")').click()
b.find_element_by_android_uiautomator('new UiSelector().className("")') 用法可参考a(classNameContains())
find_element_by_android_uiautomator('new UiSelector().classNameContains("")')
c.find_element_by_android_uiautomator('new UiSelector().resourceId("")') #
find_element_by_android_uiautomator('new UiSelector().resourceIdMatches("")') #正则
d.description--(用法同1)
e.find_element_by_accessibility_id content_dec
2.1 一些appium的其他定位方式
a.特殊属性
返回值 |
API |
说明 |
uiselector |
Checked(boolean val) |
选择属性 |
uiselector |
Clickable(boolean val) |
可点击属性 |
uiselector |
Enabled(boolean val) |
Enable属性 |
uiselector |
Focuseable(boolean val) |
焦点属性 |
uiselector |
Focused(boolean val) |
当前焦点属性 |
uiselector |
Longclickable(boolean val) |
长按属性 |
uiselector |
Scrollable(boolean val) |
滚动属性 |
uiselector |
Selected(boolean val) |
背景选择属性 |
b.index和instance
索引index指在同级中的编号,在兄弟类中的主键编号。实例instance指的是整个布局文件中的编号,同一个类的同级编号。???
index
device_a.find_elements_by_android_uiautomator('new UiSelector().className("android.widget.LinearLayout")').index(0) # 'WebElement' object has no attribute 'instance'
device_a.find_element_by_android_uiautomator('new UiSelector().className("android.widget.LinearLayout")').index(0) # 也错误
instance()
device_a.find_elements_by_android_uiautomator('new UiSelector().className("android.widget.LinearLayout")').instance(0) # 运行提示错误,list has no attribute instance device_a.find_element_by_android_uiautomator('new UiSelector().className("android.widget.LinearLayout")').instance(0) # 运行提示错误,'WebElement' object has no attribute 'instance'
这里其实find_elements返回的是一个list,可以直接使用下标list[]。
c.多个属性
eg.
device_a.find_element_by_android_uiautomator('new UiSelector().className("android.view.View").text("CYDtest594")').click()
3.相对元素定位
childSelector:从当前类往下找符合条件的子类 eg.
device_a.find_element_by_android_uiautomator('new UiSelector().className("android.view.View")').childSelector('new UiSelector().xxx')
fromParent
device_a.find_element_by_android_uiautomator('new UiSelector().className("android.view.View")').fromParent('new UiSelector().xxx') 找object同一父控件的兄弟控件
先找到父元素A=
evice_a.find_element_by_android_uiautomator('new UiSelector().className("android.view.View"))
再找A下的子元素A.find_element_by_...
4.xpath定位
已微信为例
4.1 属性定位
text定位
# text定位 device.find_element_by_xpath('//*[@text="自助工具"]').click()
resource-id定位 这里的属性值不唯一,为做演示选取了该属性,下面class也是如此。
# resource-id定位 device.find_elements_by_xpath('//*[@resource-id="com.tencent.mm:id/alw"]')[0].click()
class定位
# class定位 device.find_elements_by_xpath('//*[@class="android.widget.TextView"]')[2].click()
也可以写作
device.find_elements_by_xpath('//android.widget.TextView')[2].click()
content-desc定位
此属性值为空,不做演练。
4.2contains模糊定位
属性值只有部分值唯一的时候,可以用contains匹配
例如
device.find_element_by_xpath('//*[contains(@text,"自助")]').click()
4.3 组合定位
class、content-desc、resource-id、text可以自由组合。
继续以上面的为例:class+text
device.find_element_by_xpath('//android.widget.TextView[@text="自助工具"]').click()
也可写作
device.find_element_by_xpath('//*[@class="android.widget.TextView" and @text="自助工具"]').click()
4.4 层级关系定位
先定位父元素,再去定位子元素,或者定位子元素,再通过子元素找到父元素、定位子元素->找到父元素->兄弟元素
eg.
device.find_element_by_xpath('//android.widget.TextView[@text="自助工具"]/..')
也可写作
device.find_element_by_xpath('//android.widget.TextView[@text="自助工具"]/parent::*')
或者
device.find_element_by_xpath('//android.widget.TextView[@text="自助工具"]parent::android.widget.ListView')
以上为查找子元素的父元素。
device.find_element_by_xpath('//android.widget.TextView[@text="自助工具"]/../android.widget.TextView[@text="冻结账号"]')
查找兄弟元素