python uiautomator2 常用操作

uiautomator2 安装

python uiautomator2 安装及使用-CSDN博客

一, 通过包名可以打开app

d.app_start('com.gacne.www')

打开app运行后执行代码,查看app具体信息获取包名

复制代码
d.info

二,执行等待点击

160秒内等待xpath定位的出现执行点击

d.xpath('//*[@text="此地"]').wait(160).click()

三,屏幕截图

png_path = './images/'+str(i)+'.png'

d.screenshot(png_path)

四,获取文本内容

复制代码
#第一种
d.xpath('//*[@resource-id="cache-content"]/android.view.View[3]/android.widget.TextView[2]').info.get('text')
#第二种
d.xpath('//*[@resource-id="cache-content"]/android.view.View[3]/android.widget.TextView[2]').get_text()

五,定位元素

(3)ResourceId定位

d(resourceId="com.addcn.android.house591:id/ad_banner").click()

(4)Text定位

d(text="精选").click()

(5)Description定位

d(description="..").click()

(6)ClassName定位

d(className="android.widget.TextView").click()

(7)xpath定位

d.xpath("//*[@content-desc='分享']").click()

六,通过父级定位元素

复制代码
d(resourceId="com.ss.android.ugc.aweme:id/a0b").child(className="android.widget.FrameLayout")

选择父级下的一个元素

复制代码
d(className="androidx.recyclerview.widget.RecyclerView").child(className="android.widget.FrameLayout")[0].get_text()

child_by_text 定位子元素

#定位父级下text为'xxx',class为'com.lynx.tasm.behavior.ui.text.FlattenUIText'的元素

复制代码
d(resourceId="com.ss.android.ugc.aweme:id/a0b").child_by_text('xxx',className="com.lynx.tasm.behavior.ui.text.FlattenUIText")  

child_by_description

#定位父级下description 为'xxx',class为'com.lynx.tasm.behavior.ui.text.FlattenUIText'的元素

复制代码
re = d(resourceId="com.ss.android.ugc.aweme:id/a0b").child_by_description('xxx',className="com.lynx.tasm.behavior.ui.text.FlattenUIText")

siblings

#定位text为'xxx',class为'com.lynx.tasm.behavior.ui.view.UIView'的兄弟元素

复制代码
d(text="zcdsczzx").sibling(text="xxx",className="com.lynx.tasm.behavior.ui.view.UIView")
相关推荐
Tom Boom11 分钟前
Pytest断言全解析:掌握测试验证的核心艺术
自动化测试·python·测试开发·pytest
Jamesvalley25 分钟前
【Django】性能优化-普通版
python·性能优化·django
StackOverthink30 分钟前
[特殊字符] Altair:用Python说话,让数据自己讲故事!!!
开发语言·python·其他·信息可视化
qq_527887871 小时前
ImportError: cannot import name ‘PfeifferConfig‘ from ‘transformers‘【已解决】
linux·开发语言·python
知舟不叙1 小时前
使用OpenCV和Python进行图像掩膜与直方图分析
人工智能·python·opencv·图像掩膜
woniuhuihui2 小时前
案例8 模型量化
python
tt卡丁车2 小时前
6.11打卡
python
蓝婷儿2 小时前
6个月Python学习计划 Day 21 - Python 学习前三周回顾总结
python·学习
飞翔的佩奇2 小时前
【完整源码+数据集+部署教程】安检爆炸物检测系统源码和数据集:改进yolo11-REPVGGOREPA
python·yolo·计算机视觉·毕业设计·数据集·yolo11·安检爆炸物检测
摆渡搜不到你3 小时前
PyCharm Python IDE
ide·python·pycharm