uiautomator2 - 常用 API 汇总
# 获取页面源码
d.dump_hierarchy(compressed=False, pretty=False)
UiObject
driver UiObject.info -> uiautomator2.session.UiObject
- text, textContains, textMatches, textStartsWith
- className, classNameMatches
- description, descriptionContains, descriptionMatches, descriptionStartsWith
- checkable, checked, clickable, longClickable
- scrollable, enabled, focusable, focused, selected
- packageName, packageNameMatches
- resourceId, resourceIdMatches
- index, instance
元素定位
# id
d(resourceId="iv")
# text
d(text="主页")
# 多节点通过下标定位
d(text="Add new", instance=0)
# 子节点
d(className="android.widget.ListView").child(text="Bluetooth")
# 兄弟节点
d(text="Google").sibling(className="android.widget.ImageView")
# 通过节点的位置判断 left, right, top, bottom.
d(text="Wi‑Fi").right(className="android.widget.Switch")
# 元素信息
d(resourceId="iv").info
# 是否存在 bool
d(text="Settings").exists
d(text="Settings").exists(timeout=3)
d(text="主页", packageName=package_name).exists(timeout=10) # 添加包名
# 点击
d(resourceId="iv").click()
# 获取文本
d(text="Settings").get_text()
d(text="Settings").set_text("My text...")
d(text="Settings").clear_text()
# 获取元素坐标
x, y = d(text="Settings").center()
d(resourceId="ll_titleTxt").child(resourceId="tv_title").get_text()
d(resourceId="rl_app").child(resourceId="tv_app_name", text="签到").get_text()
d(className="android.widget.ListView", resourceId="android:id/list") \
.child_by_text("Wi‑Fi", className="android.widget.LinearLayout") \
.child(className="android.widget.Switch") \
.click()
# 点击
d.click(x, y) # 点坐标
d(resourceId="iv").click() # 点元素
d(text="Settings").click(timeout=10) # 点元素 且 设置超时时间
d(text="Settings").click(offset=(0.5, 0.5)) # 通过偏移量点击
clicked = d(text='Skip').click_exists(timeout=10.0) # 如果存在则点击
# 长按
d.long_click(x,y,duration=1.5) # 长按坐标
d(text="Settings").long_click() # 长按元素
# 拖拽
# notes : drag can not be used for Android<4.3.
# drag the UI object to a screen point (x, y), in 0.5 second
d(text="Settings").drag_to(x, y, duration=0.5)
# drag the UI object to (the center position of) another UI object, in 0.25 second
d(text="Settings").drag_to(text="Clock", duration=0.25)
# 滑动
d.swipe()
d(text="Settings").swipe("right")
d(text="Settings").swipe("left", steps=10)
d(text="Settings").swipe("up", steps=20) # 1 steps is about 5ms, so 20 steps is about 0.1s
d(text="Settings").swipe("down", steps=20)
# 点击系统按键
d.press("back")
# 获取文本
d(resourceId="iv").get_text()
# 截整图
filename = 'debug/GameScreenshot/20191209_171115.png'
d.screenshot(filename)
# 截元素
im = d(text="Settings").screenshot()
im.save("settings.jpg")
xpathSelector
# 获取第一个元素
x = d.xpath(f'//*[@text="签到"]')
print(type(x)) # <class 'uiautomator2.xpath.XPathSelector'>
print(type(x.get()) # <class 'uiautomator2.xpath.XMLElement'>
# 获取该元素所有属性
print(x.get().attrib)
{
'index': '1',
'text': '签到',
'resource-id': 'com.joyame.sixduoanew:id/tv_app_name',
'package': 'com.joyame.sixduoanew',
'content-desc': '',
'checkable': 'false',
'checked': 'false',
'clickable': 'false',
'enabled': 'true',
'focusable': 'false',
'focused': 'false',
'scrollable': 'false',
'long-clickable': 'false',
'password': 'false',
'selected': 'false',
'visible-to-user': 'true',
'bounds': '[657,987][747,1048]'
}
# 获取该元素单个属性
print(x.get().attrib.get("resource-id"))
# 获取一组元素,all() 返回的是一组 <class 'uiautomator2.xpath.XMLElement'>
# 相当于 XPathSelector.get()
x.all()
# 通过下标指定
ele = x.all()[0]
ele.attrib.get("content-desc", "") # 获取属性,并设置默认值
ele.attrib["text"] = "hello" # 设置属性
ele.attrib.pop("clickable") # 删除属性
# 点击
d.xpath("//*[@content-desc='分享']").click()
# 获取文本内容
ele.text
ele.get_text()
ele.attrib.get("text")