环境:

  • Android: 4.4+
  • Python:3.7.10

uiautomator2:https://github.com/openatx/uiautomator2

安装adb

官网地址:https://developer.android.google.cn/studio/command-line/adb

准备一台开启了开发者选项的安卓手机,通过USB连接上电脑

电脑上通过以下命令查看已连接的设备

adb devices -l

(Python37) M:\python>adb devices -l
* daemon not running. starting it now on port 5037 *
* daemon started successfully *
List of devices attached
xxxxxxxx device product:oxygen model:MI_MAX_2 device:oxygen

安装 weditor

pip install --pre --upgrade weditor

(Python37) M:\python>pip install --pre --upgrade weditor
Collecting weditor
Downloading weditor-0.6.4-py3-none-any.whl (1.6 MB)
|████████████████████████████████| 1.6 MB 726 kB/s
Collecting facebook-wda>=1.0.5
Downloading facebook_wda-1.4.3-py3-none-any.whl (25 kB)
Requirement already satisfied: pillow in h:\anacondanavigator\anaconda\envs\python37\lib\site-packages (from weditor) (8.2.0)



Successfully installed facebook-wda-1.4.3 pywin32-301 weditor-0.6.4

启动 weditor

python -m weditor

注:命令行直接输入 weditor会自动打开浏览器,输入设备的ip或者序列号,点击Connect即可。

默认会通过浏览器打开页面:http://localhost:17310/

初始化所有的已经连接到电脑的设备

python -m uiautomator2 init

或者 初始化所有的已经连接到电脑的设备 并指定端口号

python -m uiautomator2 init --addr :7912

(Python37) C:\Users\vvcat>python -m uiautomator2 init --addr :7912
[I 210917 13:27:42 init:156] uiautomator2 version: 2.16.7
[I 210917 13:27:42 init:356] Install minicap, minitouch
[D 210917 13:27:42 init:59] Use cached assets: C:\Users\vvcat.uiautomator2\cache\minitouch-f7a806902f\minitouch
[D 210917 13:27:42 init:238] Push to /data/local/tmp/minitouch:0755
[D 210917 13:27:42 init:59] Use cached assets: C:\Users\vvcat.uiautomator2\cache\minicap.so-14ea849b4c\minicap.so
[D 210917 13:27:42 init:238] Push to /data/local/tmp/minicap.so:0755
[D 210917 13:27:42 init:59] Use cached assets: C:\Users\vvcat.uiautomator2\cache\minicap-4ad740a772\minicap
[D 210917 13:27:42 init:238] Push to /data/local/tmp/minicap:0755
[D 210917 13:27:43 init:256] apk-debug package-info: {‘package_name’: ‘com.github.uiautomator’, ‘version_name’: ‘2.3.3’, ‘version_code’: ‘2003003’, ‘flags’: [‘DEBUGGABLE’, ‘HAS_CODE’, ‘ALLOW_CLEAR_USER_DATA’, ‘ALLOW_BACKUP’], ‘first_install_time’: datetime.datetime(2021, 9, 2, 19, 36, 3), ‘last_update_time’: datetime.datetime(2021, 9, 2, 19, 36, 3), ‘signature’: ‘ae17cd86’}
[D 210917 13:27:43 init:257] apk-debug-test package-info: {‘package_name’: ‘com.github.uiautomator.test’, ‘version_name’: ‘’, ‘version_code’: ‘’, ‘flags’: [‘DEBUGGABLE’, ‘HAS_CODE’, ‘ALLOW_CLEAR_USER_DATA’, ‘ALLOW_BACKUP’], ‘first_install_time’: datetime.datetime(2021, 9, 2, 19, 36, 10), ‘last_update_time’: datetime.datetime(2021, 9, 2, 19, 36, 10), ‘signature’: ‘ae17cd86’}
[I 210917 13:27:43 init:374] Already installed com.github.uiautomator apks
[D 210917 13:27:43 init:167] Shell: (’/data/local/tmp/atx-agent’, ‘server’, ‘–stop’)
[D 210917 13:27:43 init:296] Real version: [0, 10, 0], Expect version: [0, 10, 0]
[D 210917 13:27:43 init:167] Shell: (’/data/local/tmp/atx-agent’, ‘server’, ‘–nouia’, ‘-d’, ‘–addr’, ‘:7912’)
[I 210917 13:27:43 init:384] Check atx-agent version
[D 210917 13:27:43 init:397] Forward: local:tcp:1457 -> remote:tcp:7912
[D 210917 13:27:44 init:397] Forward: local:tcp:1457 -> remote:tcp:7912
[D 210917 13:27:45 init:400] atx-agent version 0.10.0
[D 210917 13:27:45 init:403] device wlan ip: 192.168.31.106
Successfully init AdbDevice(serial=xxxxxxxx)

测试:
打印通过usb连接获取到的手机信息

import uiautomator2 as ddef connect():#通过usb连接connect_usb = d.connect_usb('28f97048')print(connect_usb.info)if __name__=='__main__':connect()

(Python37) M:\python>python main.py
{‘currentPackageName’: ‘com.miui.home’, ‘displayHeight’: 1920, ‘displayRotation’: 0, ‘displaySizeDpX’: 393, ‘displaySizeDpY’: 698, ‘displayWidth’: 1080, ‘productName’: ‘oxygen’, ‘screenOn’: True, ‘sdkInt’: 25, ‘naturalOrientation’: True}

uiautomator2:https://github.com/openatx/uiautomator2/blob/master/README.md

找到 package 名字

启动app

d.app_start("com.bilibili.ydydja.bili")

关闭app

d.app_stop("com.bilibili.ydydja.bili")

解锁屏幕

def open_the_screen():d.info.get('screenOn') # 获取当前屏幕状态d.screen_on()   # 打开屏幕d.swipe_ext("up", scale=0.8)  # 默认0.9, 滑动距离为屏幕宽度的80%   从下往上滑动d.click(0.497, 0.531)   # 解锁界面数字坐标d.click(0.502, 0.818)   # 解锁界面数字坐标d.click(0.778, 0.629)   # 解锁界面数字坐标d.click(0.778, 0.629)   # 解锁界面数字坐标
  • click():点击屏幕
  • screen_on():打开屏幕
  • screen_off():关闭屏幕
  • swipe_ext():手指滑动
    • left:左滑
    • right:右滑
    • up:上滑
    • down:下滑
  • info.get():获取当前屏幕状态

使用 uiautomator2相关推荐

  1. 手机APP自动化之uiautomator2 +python3 UI自动化

    题记: 之前一直用APPium直到用安卓9.0  发现uiautomatorviewer不支持安卓 9.0,点击截屏按钮 一直报错,百度很久解决方法都不可以,偶然间看见有人推荐:uiautomator ...

  2. python uiautomator2 toast_appium+uiautomator2定位toast元素失败,求解决方案

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 appium1.15 Python3.7 安卓设备:5.1.1 定位时报错,找不到元素 配置:desired_caps = {} desired_caps ...

  3. PHP 不跳转界面取input值进行验证_【Python】tesseract+uiautomator2+夜神模拟器 悠长假期手游集市识别验证码自动购买 - Amorius...

    开宗明义:这篇文章实际上就是把我的操作记录一遍,以防以后忘记了,又要到处去搜罗.由于我是个python小白,所以这些操作都是各处学来拼到一起的,也因此我确信如果不赶紧记下来,很快就会忘掉.于是就趁热写 ...

  4. android UI定位工具-uiautomator2的使用

    介绍: uiautomator2 是一个可以使用Python对Android设备进行UI自动化的库.其底层基于Google uiautomator,Google提供的uiautomator库可以获取屏 ...

  5. python实现手机通讯录_python+uiautomator2实现需求:从通讯录添加手机号码作为SOS紧急号码...

    # 实现需求:从通讯录添加手机号码,若存在旧的SOS号码,先删除再添加,若通讯录中没有号码,需先新建. import uiautomator2as u2 from timeimport sleep i ...

  6. python模拟通讯录的删除功能_python+uiautomator2 实现需求:从通讯录添加手机号码,若存在旧的 SOS号码,先删除再添加,若通讯录中没有号码,需先新建。...

    # 实现需求:从通讯录添加手机号码,若存在旧的SOS号码,先删除再添加,若通讯录中没有号码,需先新建. import uiautomator2 as u2 from time import sleep ...

  7. Py之uiautomator2:uiautomator2的简介、安装、使用方法之详细攻略

    Py之uiautomator2:uiautomator2的简介.安装.使用方法之详细攻略 目录 uiautomator2的简介 uiautomator2的安装 uiautomator2的使用方法 1. ...

  8. uiautomator2进阶

    点击控件的具体位置 d(text="Settings").click(offset=(0.5, 0.5))   点击控件的中间位置 d(text="Settings&qu ...

  9. Python笔记-uiautomator2截图点击,OpenCV找图

    对应的代码如下: import timeimport uiautomator2 as u2 import cv2 import numpy as npd = u2.connect_usb(" ...

最新文章

  1. WebService在开发中的实际问题
  2. Struts2 JSON
  3. Document 对象描述
  4. Linux SSH命令使用大全
  5. python与excel互通_【python】python vs Excel ( 与mysql数据库之间的交互)
  6. ubuntu12.04安装教程
  7. 【Vue】路由Router传参的两种方式(详解)
  8. 方法大纲_社会工作师(中级)中级实务 考试大纲 附使用方法
  9. ORACL-重做日志详解
  10. 为什么越来越多的企业选择云数据存储而放弃本地数据存储?
  11. 联想y7000 Linux显卡驱动,联想Y7000安装显卡驱动
  12. IOS和安卓展示时间兼容问题
  13. #码神心得_03# 浮点类型、char类型
  14. wireshark抓包后查看数据报时的过滤规则/过滤语法及举例说明
  15. opencv c++实现棋盘格标定
  16. 烟台大学计算机学院王文学,杨玉军-烟台大学 数学与信息科学学院
  17. JS 实现数字滚动变化效果
  18. Codeforces Round #143 (Div. 2) (ABCD 思维场)
  19. IEEE802.1 802.11协议标准
  20. ST-FIFO的一个应用 debug

热门文章

  1. 绝佳时机,前所未遇,开创全新购物体验
  2. sqlmap工具命令行的含义
  3. Android5.1.1 - zygote中获取系统服务时抛出异常
  4. Visual Studio 2015 速递(4)——高级特性之移动开发
  5. 系统管理员易犯错误及解决方法汇总
  6. SQL Server 事务日志的问题
  7. HTML5基础-Mark标签高亮显示文本
  8. 汇编语言--sbb指令
  9. Linux日志系统-05:案例1-实现临时日志文件滚动分割
  10. QQ采用什么传输协议?