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")
相关推荐
CaracalTiger2 小时前
什么是Clawdbot?Clawdbot下载、安装、配置教程(最新版Moltbot)
python·编辑器·aigc·idea·ai编程·intellij idea·agi
WJX_KOI7 小时前
Open Notebook 一个开源的结合AI的记笔记软件
python
0思必得08 小时前
[Web自动化] 反爬虫
前端·爬虫·python·selenium·自动化
2301_822382768 小时前
Python上下文管理器(with语句)的原理与实践
jvm·数据库·python
喵手8 小时前
Python爬虫实战:从零搭建字体库爬虫 - requests+lxml 实战采集字体网字体信息数据(附 CSV 导出)!
爬虫·python·爬虫实战·零基础python爬虫教学·csv导出·采集字体库数据·字体库字体信息采集
2301_790300969 小时前
Python深度学习入门:TensorFlow 2.0/Keras实战
jvm·数据库·python
程序员敲代码吗10 小时前
用Python生成艺术:分形与算法绘图
jvm·数据库·python
Yyyyy123jsjs10 小时前
如何通过免费的外汇API轻松获取实时汇率数据
开发语言·python
喵手10 小时前
Python爬虫实战:GovDataMiner —— 开放数据门户数据集元数据采集器(附 CSV 导出)!
爬虫·python·爬虫实战·python爬虫工程化实战·零基础python爬虫教学·open data·开放数据门户数据集列表