代码有更新,适配新版支付宝,参见最新文章:

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

一、环境准备

首先,你需要一个能够运行代码的环境,这里包括:

Node.js:安装并配置环境变量

JAVA:安装并配置环境变量

Android:安装并配置环境变量

Python:安装并配置环境变量

Appium-Windows-Desktop:安装

Appium-Python-Client:在Pycharm中安装

安卓模拟器(Genymotion或MuMu都可以)或者一部安卓手机

二、实现原理

这里用到的知识主要包括:(都是一些比较简单和基础的知识,初学Appium的同学可以用来练习)

Appium对于元素以及属性的定位

基本的点击和滑动操作

关键元素特征的判断和识别

三、实现思路

1.利用appium获取支付宝APP的包名以及入口的Activity,用来启动APP

2.打开APP后进入蚂蚁森林

3.首先收取自己的能量(这里没有做过多的判断,直接把能量球可能出现的区域点击一遍)

4.然后进入到好友列表,获取好友列表中的所有好友

5.依次点击当前页面好友列表中的好友,进入好友的蚂蚁森林,收取能量(这里也没有做过多的判断,不管有没有能量,遍历所有好友)

6.当前页面的好友全部收完之后,滑动到下一页,继续收取,直至好友列表遍历结束。

四、实现代码

from appium import webdriver
import timedesired_caps = {}desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '7.1.1'
desired_caps['deviceName'] = '这里填写自己手机的设备名'
desired_caps['noReset'] = True
desired_caps['appPackage'] = 'com.eg.android.AlipayGphone'
desired_caps['appActivity'] = 'AlipayLogin'driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
time.sleep(10)def get_screen_size():x = driver.get_window_size()['width']  # 获取屏幕宽度y = driver.get_window_size()['height']  # 获取屏幕高度return (x, y)# 在首页找到【蚂蚁森林】的入口,点击进入
def get_home_view_by_text_then_click(driver,tag):home_app_view_list = driver.find_elements_by_id('com.alipay.android.phone.openplatform:id/home_app_view')home_len = len(home_app_view_list)for i in range(0,home_len):home_app_view_text = home_app_view_list[i].find_element_by_id('com.alipay.android.phone.openplatform:id/app_text').textprint(home_app_view_text)for i in range(0,home_len):home_app_view_text = home_app_view_list[i].find_element_by_id('com.alipay.android.phone.openplatform:id/app_text').textif home_app_view_text == tag:print('-------------------------------------')print(str(tag) + '->' + str(i))print('点击【' + home_app_view_text + '】')home_app_view_list[i].click()break# 通用的查找指定元素并点击的方法
def get_common_view_by_class_then_click(driver,tag):common_app_view_list = driver.find_elements_by_class_name('android.view.View')common_len = len(common_app_view_list)# for i in range(0,common_len):#     common_app_view_text = common_app_view_list[i].get_attribute('name')#     if common_app_view_text:#         print(common_app_view_text)for i in range(0,common_len):common_app_view_text = common_app_view_list[i].get_attribute('name')if common_app_view_text == tag:print('-------------------------------------')print(str(tag) + '->' + str(i))print('点击【' + common_app_view_text + '】')common_app_view_list[i].click()break# 实现一次按坐标进行的点击
def tap_alone(x,y,duration=1000):l = get_screen_size()width = int(l[0])  # 获取屏幕宽度height = int(l[1])  # 获取屏幕高度tap_x1 = int((int(x) / width) * width)tap_y1 = int((int(y) / height) * height)print('点击坐标:['+str(tap_x1)+', '+str(tap_y1)+']')driver.tap([(tap_x1, tap_y1), (tap_x1, tap_y1)], duration)# 实现一次收取能量
def tap_get_energy():# 能量球可能出现的区域坐标start_x = 120start_y = 470end_x = 920end_y = 870print('开始收取能量>>>')# 依次点击指定区域内的等距离坐标点for i in range(start_y,end_y,80):for j in range(start_x,end_x,80):print('['+str(j)+', '+str(i)+']',"\t", end="")tap_alone(j,i)print()# 遍历获取好友列表
def get_friend_view_by_class_then_click(driver):time.sleep(1)for i in range(0,16):print(str(i))swipeUp(2000,500,3000)for i in range(0,16):print(str(i))swipeDown(500,2000,3000)time.sleep(1)swipeUp(2000, 1800)time.sleep(1)friend_view_square = driver.find_element_by_id('J_rank_list')friend_view_list = friend_view_square.find_elements_by_class_name('android.view.View')common_len = len(friend_view_list)print(common_len)print('-------------------------------------')count = 0for i in range(0,common_len):friend_view_view_text = friend_view_list[i].get_attribute('name')if friend_view_view_text:# print(friend_view_view_text)if count%10 == 0 and count != 0:count = 0print('向上划一页')swipeUp(2000, 120)time.sleep(1)# if '获得了' in friend_view_view_text and count<10:if 'g' in friend_view_view_text:print('-------------------------------------')print('收取【'+friend_view_list[i-2].get_attribute('name')+'】的能量>>>')friend_view_list[i].click()count += 1# print(count)time.sleep(1)tap_get_energy()driver.press_keycode(4)# for i in range(0,common_len):#     friend_view_view_text = friend_view_list[i].get_attribute('name')#     if friend_view_view_text:#         print('-------------------------------------')#         print(str(friend_view_view_text) + '->' + str(i))#         print('点击【' + friend_view_view_text + '】')#         friend_view_list[i].click()# 实现安坐标精准滑动:向上滑动
def swipeUp(y1,y2,duration=5000):l = get_screen_size()width = int(l[0])  # 获取屏幕宽度height = int(l[1])  # 获取屏幕高度x1 = int(width * 0.5)y1_start = int((int(y1) / height) * height)y2_end = int((int(y2) / height) * height)driver.swipe(x1, y1_start, x1, y2_end, duration)print('向上滑动【'+str(y1_start-y2_end)+'】')# 实现安坐标精准滑动:向下滑动
def swipeDown(y1,y2,duration=5000):l = get_screen_size()width = int(l[0])  # 获取屏幕宽度height = int(l[1])  # 获取屏幕高度x1 = int(width * 0.5)y1_start = int((int(y1) / height) * height)y2_end = int((int(y2) / height) * height)driver.swipe(x1, y1_start, x1, y2_end, duration)print('向下滑动【'+str(y2_end-y1_start)+'】')# 点击【蚂蚁森林】
get_home_view_by_text_then_click(driver,'蚂蚁森林')
time.sleep(5)# 收取自己的能量
tap_get_energy()
time.sleep(3)# 滑动屏幕,找到【好友排行榜】
swipeUp(2000, 100)
time.sleep(1)
swipeUp(2000,1100)
time.sleep(1)
# 点击进入【好友排行榜】
get_common_view_by_class_then_click(driver,'查看更多好友')
# 开始收取好友列表中好友的能量
get_friend_view_by_class_then_click(driver)time.sleep(5)
driver.quit()

五、运行结果

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

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

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

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

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

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

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

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

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

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

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

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

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

  7. Autojs实现蚂蚁森林自动收取能量球

    警告: 本文较为基础,大佬绕行,不喜勿喷 下面正式开始步骤 一:安装Autojs 开源下载链接:https://github.com/Ericwyn/Auto.js/releases 在Github上 ...

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

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

  9. 蚂蚁森林自动收能量的最新脚本

    环境必须是安卓7.0以上版本.中文界面. 安装软件,执行Auto.js代码.完成.注意自启动,锁定后台,无障碍服务,悬浮窗等权限允许. 注意需要把脚本和图片放到执行文件夹的同一目录下. 首先所有资料链 ...

最新文章

  1. oracle notes,Oracle Notes
  2. ASP.NET Core Web API下事件驱动型架构的实现(二):事件处理器中对象生命周期的管理
  3. LeetCode 110. 平衡二叉树思考分析
  4. python3.6.5+cuda9+cudnn7.1+win10+tensorflow-gpu1.9.0下载配置
  5. 结合中国古典文化取名 华为意在把传说化为现实奇迹
  6. VMware vSAN 技术详解 | 周末送资料
  7. 美国太空部队加入美国情报系统,以确保太空的安全
  8. mysql 行转列 列转行
  9. 如何在virtualenv环境中安装指定的python版本
  10. nginx 代理springmvc到二级目录_详解Nginx从入门到实践
  11. matlab连续时间系统复频域分析,实验五连续时间信号与系统的复频域分析的MATLAB实现.doc...
  12. 《隋唐演义》二:竞争对手的实力在不断增强
  13. 1到100的平方和 java,java 1个数等于几个数平方和
  14. 华为NP课程笔记26-VXLAN概述
  15. java的小区车辆管理系统_基于Java的小区车辆信息管理系统的设计与实现
  16. php丢色子,jQuery+PHP实现的掷色子抽奖游戏实例_PHP
  17. C# 短消息提示 窗口位置
  18. 百度在线笔试计算机视觉,[转载]2014 百度 计算机视觉笔试
  19. 伦敦国王学院计算机申请要求,伦敦大学国王学院计算机科学与管理本科申请条件.pdf...
  20. python flask热更新_客户端python热更新

热门文章

  1. 树莓派3B Qt+dht11读取温湿度并写入数据库202005(8)
  2. 【征稿】第三届电子、信息与计算技术前沿国际会议(ICFEICT 2023)
  3. C++沙海拾遗(三)
  4. 交换机 tagged 与 untagged 的关系
  5. 白杨SEO:如何快速收集百度、抖音、知乎、小红书等关键词搜索下拉词及挖掘更精准长尾关键词?
  6. Hwclock用法介绍
  7. 虚拟机windows10 屏幕大小不能自适应
  8. 数位DP --Windy数
  9. 选择软件开发平台,“快速开发”是关键
  10. CenterNet:Objects as Points代码解析(九):通过高斯函数画热点图