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")
相关推荐
xiangjiaodalishi8 小时前
武汉玖叁鹿科技:我们把 GEO 服务做成了可以审计的交付工程
python·1024程序员节
zzu123zsw8 小时前
为什么 Python 做 Agent 总在并发中崩溃?拆解 Blockcell 在 Rust 下的 Host-Cell 隔离与自愈状态机
开发语言·python·rust·大模型·ai agent
jiangliushi68 小时前
GPT-6.1 Sol 免费用:Python 读文件报 ‘gbk‘ codec can‘t decode?加一个参数就好
python·编码·1024程序员节
餐赢计餐饮策划设计9 小时前
如何判断国潮养生设计是否适合自己?
python·1024程序员节
艾灸爱好者9 小时前
艾草精油专业制造商哪3点决定品质高低?
python·1024程序员节
weixin_440401699 小时前
大数据技术-人工智能编程题
大数据·人工智能·python·1024程序员节
“AI国潮设计-小江”10 小时前
AIGC实战:Python+SDXL批量生成英歌舞IP立牌,解锁猪猪蛋糕“反差萌”流量密码(附完整API源码)
开发语言·人工智能·python·prompt·aigc
成信吴彦祖10 小时前
当前专业的无人机RID侦测主站供应商主要有哪些
python·1024程序员节
编程的一拳超人10 小时前
【元宝搜索开发者征文大赛】四语实战:Java/Python/Go/Node.js 构建 AI 搜索应用全攻略
java·python·golang·node.js·agent
fanruitian10 小时前
python anaconda jupyter notebook
python