ATX-uiautomator2 使用 QPython 在 Android 手机内执行自动化 -及wsl安装使用【未成功】

吐槽一下,明明github已经是微软的了,为什么用微软的edge去访问就显示“无法安全地连接到此页面 这可能是因为该站点使用过期的或不安全的 TLS 安全设置。如果这种情况持续发生,请与网站的所有者联系。” 相关设置中与tls相关的都打了勾,网络也重置过了,死活不行。然而,换了chrome就正常访问了。怪了!

不想用autojs(accessbilityservice) 、按键精灵之类的,参考https://testerhome.com/topics/11980试试uiautomator2和androidhelper

1、手机安装Qpython

官网https://www.qpython.com,下载地址:https://github.com/qpython-android/qpython3/releases  (可能需ladder)

解压出APK,安装到手机上(复制到手机上安装或adb安装) 

2 、然而手机连不上QPYPI!在QPython的终端执行os.system('adb')是可以的(手机是安卓5.1.1),但用pip3但换不了源,下载不了东西。

3、然后想手机上能否执行ADB,参考https://blog.csdn.net/sinat_22657459/article/details/90207753,在https://github.com/bavelee/ADBToolKitsInstaller下了个apk,结果也是不能解析包,手机也没root!还是不行。

4、参考https://blog.csdn.net/qq_41664526/article/details/93895073  先在电脑上需要的库装好,结果又提示某个库不支持windows

好吧,用win10 wsl试试。

4.1    安装 win10 wsl

参考https://blog.csdn.net/weixin_40955163/article/details/100555823   到microsoft store下载ubuntu ,结果又提示:你所在的地区不支持通过Microsoft.com购买

4.2  困难重重,在win10的 设置-时间和语言-区域 改为香港。重新打开microsoft store 可以下载了。又弹出个传递优化,选择同意从其他电脑下载(意思是别人也可从我的电脑下载?)果然几秒钟就好了。

4.3 安装完毕之后我们打开

控制面板 -> 程序和功能 -> 启用或关闭Windows功能 -> 勾选 适用于Linux的Windows子系统

然后我们重启系统,在开始菜单中找到Ubuntu-18.04,点击,它会显示正在安装,可能需要几分钟时间。然后可能会提示输入用户名和密码,就像平时安装Ubuntu server时一样设置就行了。然后就算安装完成了。

4.4看了下win10版本,升级wsl2就不试了。在Ubuntu中试试cd  ls   vim  python pip3 brew 按需安装...

当然首先要切为国内源,参考https://www.cnblogs.com/jylee/p/10955457.html  光是vim操作就折腾了一阵子。

阿里源:

deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

vim真难用,直接在wsl中执行  cp sources.list /mnt/d/sources.list  ,然后到D盘下用win10记事本编辑。然后再cp回去。

更改完成之后执行以下命令

apt update
apt upgrade

 终于可以安装pip3和brew了。

5 参考https://testerhome.com/topics/11980继续实践,本来准备先用电脑下载,再推送到手机上,但找不到QPypi国内源,电脑上也下载不了。

无耐啊,被逼只能参考http://16bing.com/2017/02/19/在手机上安装s,结果用时又出现“您的设备不支持googleplay服务因此无法运行“问题,还是得root手机。

最终这次尝试定义为失败!

不甘心啊,继续在未root的手机上安装老王的那个:老wan_v2.2.11.apk,可以使用QPypi了,但发现并没有几个所库可以下载。

6、继续参考https://testerhome.com/topics/11980直接在win10电脑上运行:

pip install --upgrade setuptools

接着,安装uiautomator2(如果没执行上一句,执行下一句可能出错):

pip install --pre -U uiautomator2

再,设备安装atx-agent:

python -m uiautomator2 init

最后提示success,代表atx-agent初始化成功。

7、在电脑上执行

pip install requests
pip install humanize
pip install progress
pip install retry

我的都提示为:Requirement already satisfied

8、将相关的库及脚本文件push到手机内

经过观察QPython中路径在这个下面:

此电脑8692-M02内部存储设备qpython  对应的其实应该是:/storage/emulated/0/qpython

将uiautomator2库复制到手机:

adb push D:PythonPython37Libsite-packagesuiautomator2 /storage/emulated/0/qpython/lib/python3.6/site-packages

 竟然提示:Read-only file system   

补充:后来参考百度经验,发现应该是: 

adb push D:/Python/Python37/Lib/site-packages/humanize /sdcard/qpython/lib/python3.6/site-packages/humanize

好吧,直接从电脑资源管理器中向手机内存卡复制粘贴。

同理,把上面第7步的retry等几个库也从电脑上复制到手机相应位置。

9、写脚本atx_agent_demo.py

import uiautomator2 as ut2


def main():
    u = ut2.connect('http://0.0.0.0:7912')
    print(u.info)
    u.app_start('com.netease.cloudmusic')
    u(text='私人FM').click()
    u(description='转到上一层级').click()
    u(text='每日推荐').click()
    u(description='转到上一层级').click()
    u(text='歌单').click()
    u(description='转到上一层级').click()
    u(text='排行榜').click()
    u(description='转到上一层级').click()


if __name__ == '__main__':
    main()

 将写好的自动化脚本文件atx_agent_demo.py复制到qpython/scripts3

  打开QPython,点击文件,然后点击dcripts找到atx_agent_demo.py,运行。

提示uiautomator2库不存在,看来直接复制到site-packages目录下的方法不行。

在手机上用挂代理重新安装 setuptools  

pip3 install --upgrade setuptools

在手机上用国内镜像重新安装uiautomator2

 pip3 install -i  http://mirrors.aliyun.com/pypi/simple/ uiautomator2

出现了Command "python setup.py egg_info" failed with error code 1

再次失败。看来还是应该root手机!或者python版本不对?

参考:https://testerhome.com/topics/11980

https://blog.csdn.net/sinat_22657459/article/details/90207753

https://blog.csdn.net/qq_41664526/article/details/93895073

https://www.cnblogs.com/yjlch1016/p/8641910.html

https://www.jianshu.com/p/96d858f140ea

https://blog.csdn.net/wjx_1999/article/details/103808862

原文地址:https://www.cnblogs.com/pu369/p/12752880.html