环境配置与准备
安装uiautomator2,好像weditor也是要手动装一下,记不清了。
安卓手机打开开发者选项,打开usb调试、usb安装等选项。
weditor查看手机页面元素
进入配置的conda环境,打开weditor
python -m weditor
左侧可直接点击索引元素,中间显示被选中的元素info,右上可以调试测试代码,右下可以按照结构索引元素。
uiautomator方法
连接手机
import uiautomator2 as u2
d = u2.connect() # connect to device
print(d.info)
这种方法只能通过usb连接一个手机,info方法用来显示元素的info文章来源:https://www.toymoban.com/news/detail-617062.html
两大键方法
d.press("home")
d.press("back")
打开/关闭某app
d.app_stop('cn.soulapp.android')
d.app_start("cn.soulapp.android")
点击某元素
先找到需要点击的元素,然后使用click()方法文章来源地址https://www.toymoban.com/news/detail-617062.html
d(text='聊天', className='android.widget.TextView').click()
在文本框中输入文本
d(focused=True).send_keys("阿斯蒂芬")
可用于定位元素的属性
"text": (0x01, None), # MASK_TEXT,
"textContains": (0x02, None), # MASK_TEXTCONTAINS,
"textMatches": (0x04, None), # MASK_TEXTMATCHES,
"textStartsWith": (0x08, None), # MASK_TEXTSTARTSWITH,
"className": (0x10, None), # MASK_CLASSNAME
"classNameMatches": (0x20, None), # MASK_CLASSNAMEMATCHES
"description": (0x40, None), # MASK_DESCRIPTION
"descriptionContains": (0x80, None), # MASK_DESCRIPTIONCONTAINS
"descriptionMatches": (0x0100, None), # MASK_DESCRIPTIONMATCHES
"descriptionStartsWith": (0x0200, None), # MASK_DESCRIPTIONSTARTSWITH
"checkable": (0x0400, False), # MASK_CHECKABLE
"checked": (0x0800, False), # MASK_CHECKED
"clickable": (0x1000, False), # MASK_CLICKABLE
"longClickable": (0x2000, False), # MASK_LONGCLICKABLE,
"scrollable": (0x4000, False), # MASK_SCROLLABLE,
"enabled": (0x8000, False), # MASK_ENABLED,
"focusable": (0x010000, False), # MASK_FOCUSABLE,
"focused": (0x020000, False), # MASK_FOCUSED,
"selected": (0x040000, False), # MASK_SELECTED,
"packageName": (0x080000, None), # MASK_PACKAGENAME,
"packageNameMatches": (0x100000, None), # MASK_PACKAGENAMEMATCHES,
"resourceId": (0x200000, None), # MASK_RESOURCEID,
"resourceIdMatches": (0x400000, None), # MASK_RESOURCEIDMATCHES,
"index": (0x800000, 0), # MASK_INDEX,
"instance": (0x01000000, 0) # MASK_INSTANCE,
到了这里,关于使用uiautomator2控制安卓手机的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!