1准备环境

  Window10系统

  Appium1.21.0

  AndroidSDK r24.1.1

  Python3.7.5

  支付宝apk文件

2查看支付宝apk包信息

使用android sdk aapt命令查看支付宝apk包信息,后面会用到,如下。

Android Asset Packaging ToolUsage:aapt l[ist] [-v] [-a] file.{zip,jar,apk}List contents of Zip-compatible archive.aapt d[ump] [--values] [--include-meta-data] WHAT file.{apk} [asset [asset ...]]strings          Print the contents of the resource table string pool in the APK.badging          Print the label and icon for the app declared in APK.permissions      Print the permissions from the APK.resources        Print the resource table from the APK.configurations   Print the configurations in the APK.xmltree          Print the compiled xmls in the given assets.xmlstrings       Print the strings of the given compiled xml assets.D:\android-sdk-windows\build-tools\29.0.0> .\aapt.exe dump badging C:\Users\XXX\Downloads\alipay_wap_main.apk输出结果如下:
package: name='com.eg.android.AlipayGphone'
versionCode='410'
versionName='10.2.26.9000'
compileSdkVersion='29'
compileSdkVersionCodename='10'
install-location:'auto'
sdkVersion:'18'
targetSdkVersion:'29'
launchable-activity: name='com.eg.android.AlipayGphone.AlipayLogin'

3检测设备是否连接

一开始想用模拟器(如夜神模拟器)进行自动化,后来发现支付宝在模拟器里运行特别卡,最终决定使用真机了。这里使用android sdk的adb工具检测手机设备是否连接正常,如下。如果看不到连接信息或者显示unauthorized的,请开启手机的USB调试权限,也有可能开启开发者模式呦。

4准备知识

pip install 默认安装路径修改

Appium工作原理

Appium使用Python运行appium测试的实例

5安装自动化工具包

pip install Appium-Python-Client --user

An extension library for adding Selenium 3.0 draft and Mobile JSON Wire Protocol Specification draft functionality to the Python language bindings, for use with the mobile testing framework Appium.

pip install pytest --user

The pytest framework makes it easy to write small tests, yet scales to support complex functional testing for applications and libraries.

安装包的时候加上参数--user 包就会自动安装到自定义路径下面

6启动Appium服务端

7开启新会话定位画面元素

{"deviceName": "Alipay-Test","platformName": "Android","platformVersion": "10","appActivity": "com.eg.android.AlipayGphone.AlipayLogin","appPackage": "com.eg.android.AlipayGphone","noReset": true,"fullReset": false
}

8编写python脚本

知道如何定位支付宝界面的元素后,开始编写python自动化运行脚本。大体分为如下几个步骤。

8.1初始化客户端

def setUp(self):desired_caps = {}desired_caps['platformName'] = 'Android'desired_caps['platformVersion'] = '10'desired_caps['deviceName'] = 'Alipay'desired_caps['appActivity'] = 'com.eg.android.AlipayGphone.AlipayLogin'desired_caps['appPackage'] = 'com.eg.android.AlipayGphone'desired_caps['noReset'] = Truedesired_caps['fullReset'] = Falseself.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

客户端初始化后会自动启动支付宝APP,注意noReset和fullReset参数的设置。

If noReset is set to TRUE, the app data will NOT be cleared before this session starts.
If fullReset is set to true, the app will get uninstalled and all data will be cleared.

8.2解锁手机

def unlocked(self):sz = self.getSize();x1 = int(sz[0] * 0.10)      #x坐标y1 = int(sz[1] * 0.95)      #起始y坐标y2 = int(sz[1] * 0.15)      #终点y坐标self.driver.swipe(x1, y1, x1, y2, 1000)sleep(1)try:self.driver.find_element_by_id('com.android.systemui:id/vivo_pin_keyboard')for k in [5,1,2,9,9,9]:self.driver.find_element_by_id('com.android.systemui:id/VivoPinkey%d' % k).click()print('手机解锁成功...')except NoSuchElementException:print('手机已解锁或解锁失败')

8.3进入蚂蚁森林

def entry_ant_forest(self):try:sleep(2)# 点击蚂蚁森林iconself.driver.find_element_by_android_uiautomator('new UiSelector().text("蚂蚁森林")').click()except NoSuchElementException:# 异常回到首页重试self.driver.back()sleep(2)# 点击支付宝iconself.driver.find_element_by_android_uiautomator('new UiSelector().text("支付宝")').click()sleep(2)# 点击蚂蚁森林iconself.driver.find_element_by_android_uiautomator('new UiSelector().text("蚂蚁森林")').click()

按理说进入蚂蚁森林直接模拟点击“蚂蚁森林”icon就可以了,但是偶尔会抛出NoSuchElementException异常。也就是Appium在切换activity后导致元素无法定位,如果手机不锁屏不会发生这种情况(可以在开发者模式中指定),锁屏解锁后切换到支付宝的activity后偶尔会出现这种情况。没有找到太好的解决方法,发生异常时使手机界面返回到首页,然后点击支付宝重新进入,最后点击蚂蚁森林进入。

8.4搜索能量

def search_energy(self):# 点击找能量self.driver.tap([(1000, 1520), (1080, 1580)], 1000)sleep(3)try:self.driver.find_element_by_android_uiautomator('new UiSelector().textContains("好友能量都收完了")')except NoSuchElementException:try:self.driver.find_element_by_android_uiautomator('new UiSelector().textContains("返回我的森林")')except NoSuchElementException:passelse:print('全部好友能量收取完毕...')return# 收取好友能量self.collect_energy()# 继续找能量self.search_energy()else:print('全部好友能量收取完毕...')

点击“找能量”功能自动定位到有能量偷取的好友界面。如果界面中有“好友能量都收完了”或者“返回我的森林”相关字样,结束查找,否则开始收取好友能量。

8.5收取好友能量

def collect_energy(self):name = ''try:name = self.driver.find_element_by_id('com.alipay.mobile.nebula:id/h5_tv_title').textexcept NoSuchElementException:passprint('开始收取%s的能量...' % name)# 获取手机屏幕宽高sz = self.getSize();width = sz[0]height = sz[1]# 能量球可能出现的区域坐标start_x = 110end_x = 940start_y = 460end_y = 880for i in range(start_y, end_y, 80):for j in range(start_x,end_x, 80):try:self.driver.find_element_by_android_uiautomator('new UiSelector().textContains("关闭")').click()sleep(1)except NoSuchElementException:passtap_x1 = int((int(j) / width) * width)tap_y1 = int((int(i) / height) * height)# 点击指定坐标self.driver.tap([(tap_x1, tap_y1), (tap_x1, tap_y1)], 1000)print('结束收取%s的能量...' % name)

首先获取当前页面的“com.alipay.mobile.nebula:id/h5_tv_title”元素,代表好友的名字;因为蚂蚁森林现在不能定位能量球元素了,所以要在能量球可能出现的方块区域按照一定的坐标步长模拟点击进行能量偷取。上面的方案中,规定的能量球可能出现的区域为[(110,460),(940,880)],这个坐标可以根据实际的机型进行修改,可以通过Appium坐标定位判断出矩形区域,如下。

还要一个点需要注意的,点击的时候可能会出现装饰树和挂件的展示,如下图所示。这时候需要在界面中查找“关闭”元素,然后click事件关闭就行了。

9演示效果

如果效果感觉还不错的话,那就关注一下微信公众号(算法和技术SHARING),回复“蚂蚁森林”获取完整代码吧。

Appium和Python实现蚂蚁森林自动化收取能量相关推荐

  1. 每天都有人偷我森林的能量,我写了一个蚂蚁森林自动化收取能量!看你们咋偷!

    前言 很多小伙伴应该和小编一样,没啥事了,就会收一收蚂蚁森林的能量,就好像回到了当年种菜,农场,牧场这些一样,但是总有刁民惦记着我得能量,当年就是偷我的菜,虽然我也没少偷!哈哈哈!好了接下来教你们怎么 ...

  2. 【Appium】Python+Appium实现支付宝蚂蚁森林自动收取能量的一种解决方案

    代码有更新,适配新版支付宝,参见最新文章: [Appium][更新]Python+Appium实现支付宝蚂蚁森林自动收取能量 一.环境准备 首先,你需要一个能够运行代码的环境,这里包括: Node.j ...

  3. 【Appium】【更新】Python+Appium实现支付宝蚂蚁森林自动收取能量

    一.前期准备 环境准备和以前的脚本可以参见之前的文章[Appium]Python+Appium实现支付宝蚂蚁森林自动收取能量的一种解决方案 二.更新内容 新版的支付宝APP更新了遍历蚂蚁森林好友列表的 ...

  4. adb 查看屏幕大小_蚂蚁森林自动收取能量、偷取能量、浇水(使用adb、python)...

    涉及到的技术: 1.python 2.adb 具备的功能: 1.自动收取能量 2.自动偷取能量 3.自动给指定的朋友浇水 使用方法: 1.打开电脑,USB线一头接手机,一头接电脑. 2.电脑运行pyt ...

  5. 苹果 python蚂蚁森林自动收能量_GitHub - dxp432/adb_python_alipay_AntForest: 蚂蚁森林自动收取能量、偷取能量、浇水(使用adb、python)...

    蚂蚁森林自动收取能量.偷取能量.浇水 蚂蚁森林自动收取能量.偷取能量.浇水(使用adb.python)adb_python_alipay_AntForest 涉及到的技术: 1.python 2.ad ...

  6. 如何使用 AccessibilityService 实现蚂蚁森林自动收取能量,无需Root,无需连接电脑

    如何使用 AccessibilityService 实现蚂蚁森林自动收取能量,无需Root,无需连接电脑 AccessibilityService 设计初衷在于帮助残障用户使用android设备和应用 ...

  7. 使用Auto.js实现蚂蚁森林自动收取能量

    在网上看了一些自动收能量的脚本 根据自己的手机型号 华为荣耀9 分辨率为1980*1080 写了一个脚本 使用AutoJs运行 定时每天早上7点开始收能量(再也不用担心我的能量被偷啦 哈哈~) Aut ...

  8. 苹果 python蚂蚁森林自动收能量_蚂蚁森林自动收取能量、偷取能量、浇水(使用adb、python)...

    涉及到的技术: 1.python 2.adb 具备的功能: 1.自动收取能量 2.自动偷取能量 3.自动给指定的朋友浇水 使用方法: 1.打开电脑,USB线一头接手机,一头接电脑. 2.电脑运行pyt ...

  9. 利用python实现蚂蚁森林自动偷能量

    利用到的python库包:Uiautomator2 UiAutomator 是 Google 提供的用来做安卓自动化测试的一个 Java 库,可以获取屏幕上任意一个 APP的任意一个控件属性,并对其进 ...

  10. 分享一 AutoJs 蚂蚁森林自动收取能量和偷取能量

    首先声明,代码不是本人所写,只是做了点改动.本文的目的一是分享给大家,二算是记个笔记方便以后看. 最近看到AutoJs可以收蚂蚁森林的能量,自己也想写一个,所以就搜了几个大神的代码,在此基础上,边看边 ...

最新文章

  1. 一个在raw里面放着数据库文件的网上例子
  2. sql查询第二大的记录(转)
  3. C# 8新提案让泛型Attribute成为现实
  4. 如何强制“ git pull”覆盖本地文件?
  5. 转:IE iframe不刷新的问题之完美解决
  6. Java中,内部类的概述和内部类的访问特点和内部类的分类(内部类的位置)
  7. 【我的区块链之路】- Hyperledger fabric的简单入门(四)链码的编写及调试
  8. SAP ERP系统业务优化之采购订单追踪
  9. 和qc哪个发展更好_城西公司举办2020年度QC成果推广交流发布会
  10. 怎么去掉Xcode工程中的某种类型的警告
  11. 楼层钢筋验收会议纪要_钢筋施工质量通病防治
  12. LeetCode题库整理【Java】—— 1两数之和
  13. 搞懂nginx的proxy模块-01
  14. 十八、JAVA基本数据类型的包装类
  15. 洛克菲勒:一部西方石油工业的传奇史
  16. 修改IP、DNS、MAC工具VC源码实现
  17. Sketch 51 新功能介绍(包含破解版下载)
  18. 支付宝小程序对接流程和工具类
  19. STM32与串口屏交互(USART HMI)
  20. 南阳oj 语言入门 房间安排

热门文章

  1. 怎么样添加桌面我的计算机,怎么样把我的电脑添加到桌面上
  2. 使用docker搭建个人博客
  3. hotmail手机端_hotmail邮箱登陆手机版 参见http://help.
  4. vs2019编译cryengine失败问题处理
  5. windows提升效率神器
  6. Codelf 命名神器
  7. 2018-8-10-win10-uwp-使用资源在后台创建控件
  8. 详解InnoDB的Buffer Pool
  9. 用微信公众号写博客就是玩,要动真格的还是得WordPress!
  10. android的listview分组显示的时候layout_marginTop失效的解决办法