''''
1、安装airtest(pip install airtest)
2、安装poco(pip install poco)
3、安装pocoui(pip install pocoui)
4、环境:华为MATE8\PYTHON:3.7\airtest:1.1.2
5、参考网站
https://github.com/MiracleYoung/You-are-Pythonista/blob/master/PythonExercise/App/Grab_LuckyMoney/Grab_LuckyMoney.py
http://www.mamicode.com/info-detail-2748922.html
https://www.bilibili.com/video/av61723432/?spm_id_from=trigger_reload
'''
from airtest.core.api import *
auto_setup(__file__)
from poco.drivers.android.uiautomation import AndroidUiautomationPoco
poco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False)
def Grab_LuckyMoney():
# 获取消息列表元素
#CurrentPage_msg_list = poco("android.widget.ListView").children()
CurrentPage_msg_list = poco("android.widget.FrameLayout").children()
# 把获取到的消息,反序存储,以便从最新的红包开始抢起
msg_list = []
for i in CurrentPage_msg_list:
msg_list.insert(0, i)
# 遍历消息并查找红包
for msg in msg_list:
# 微信红包的标识,注意定位
#LuckyMoney = msg.offspring('com.tencent.mm:id/aql////com.tencent.mm:id/auk///com.tencent.mm:id/auj')
LuckyMoney = msg.offspring('com.tencent.mm:id/auj')
# 已失效红包(比如已领取、已被领完)的标识
#Invalid = msg.offspring('com.tencent.mm:id/aqk')
Invalid = msg.offspring('com.tencent.mm:id/aul')
# 判断红包是否有效并抢起来!
if LuckyMoney:
print(f'红包!红包!')
if Invalid.exists() and (Invalid.get_text()=='已领取' or Invalid.get_text()=='已被领取'):
print(f'红包已无效,跳过……')
continue
else:
print(f'发现一个新红包,抢起来!')
sleep(1.5)
#poco("com.tencent.mm:id/d1v")
poco("com.tencent.mm:id/bah")
msg.click()
#点开红包
sleep(1.5)
poco(name='com.tencent.mm:id/auj').click()
#click_open = poco("com.tencent.mm:id/d02")
#领取红包
click_open = poco(name='com.tencent.mm:id/pq')
if click_open.exists():
click_open.click()
keyevent('BACK')
else:
print(f'未发现红包……')
continue
if __name__ == '__main__':
"""
# 查看当前手机上的app的包,有哪些,找到需要打开的包
dev = device()
# 获取所以的应用的包名:com.tencent.mm 为微信的包名
print(dev.list_app(third_only=True))
"""
# 打开app
# start_app('com.tencent.mm')
# 打开手机微信
#poco(text='微信').click()
#
# 群聊消息的元素标识
#Chat_msg = poco(name='com.tencent.mm:id/d1v').offspring('com.tencent.mm:id/b6e')#offspring 子孙标识
Chat_msg = poco(name='com.tencent.mm:id/bah').offspring('com.tencent.mm:id/baj')
# 获取当前页面中所有所有群聊的名称
Chat_names = []
Chat_names = list(map(lambda x: x.get_text(), Chat_msg))
""""
Chat_names = []
for item in items_elements:
name = item.get_text()
Chat_names.append(name)
print(Chat_names)
"""
# 指定抢红包的群聊名称
#chat = input('请指定群聊名称:')
chat = '一家亲'
if chat in Chat_names:
index = Chat_names.index(chat)
# 点击进入指定的群聊
Chat_msg[index].click()
while True:
Grab_LuckyMoney()
print(f'休眠3秒钟,继续刷新页面,开始抢红包')
sleep(1)
else:
print(f'找不到这个群')
过程常见的坑
1、定位不准确