脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python+Appium实现自动抢微信红包

Python+Appium实现自动抢微信红包

2021-11-10 10:05HUNY Python

不知从何时开始微信红包横空出世,对于网速和手速慢的人只能在一旁观望,做为python的学习者就是要运用编程解决生活和工作上的事情。于是我用python解决我们的手速问题python实现自动抢微信红包,至于网速慢得那就只能自己花

环境准备

  • appium环境
  • 安卓手机
  • usb数据线
  • python环境

实现思路

我们收到红包和消息都是自动置顶到第一个,于是我们打开第一个判断是否有红包,没有则隐藏此窗口。如果有则判断红包是否可以领取,如果有则领取红包,否则删除此红包(不然会影响后面的判断)
然后再进行循环运行和判断。

Python+Appium实现自动抢微信红包

code

首先看一下配置信息,因为我使用得是真机小米9安卓10的系统,代码实现如下具体的信息填写请根据自己的真实情况修改:

?
1
2
3
4
5
6
7
8
9
desired_caps = {
    "platformname": "android"# 系统
    "platformversion": "10.0"# 系统版本号
    "devicename": "b68548ed"# 设备名
    "apppackage": "com.tencent.mm"# 包名
    "appactivity": ".ui.launcherui"# app 启动时主 activity
    'unicodekeyboard': true,  # 使用自带输入法
    'noreset': true  # 保留 session 信息,可以避免重新登录
}

因为点击红包后需要判断点击后的红包是否被领取,即是否有开字,如图所示:

Python+Appium实现自动抢微信红包

所以我们定义一个判断元素是否存在的方法,代码实现如下:

?
1
2
3
4
5
6
7
def is_element_exist(driver, by, value):
    try:
        driver.find_element(by=by, value=value)
    except exception as e:
        return false
    else:
        return true

因为红包无论是被自己领取还是被他人领取,之后都要删除领取后的红包记录,所以我们再来定义一个删除已领取红包的方法,代码实现如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
def del_red_envelope(wait, driver):
    # 长按领取过的红包
    r8 = wait.until(ec.element_to_be_clickable(
        (by.id, "com.tencent.mm:id/ahs")))
    touchaction(driver).long_press(r8).perform()
    time.sleep(1)
    # 点击长按后显示的删除
    wait.until(ec.element_to_be_clickable(
        (by.id, "com.tencent.mm:id/dt5"))).click()
    # 点击弹出框的删除选项
    wait.until(ec.element_to_be_clickable(
        (by.id, "com.tencent.mm:id/ffp"))).click()

Python+Appium实现自动抢微信红包

同时有可能第一个是公众号推送的消息,这样会导致无法判断,所以我们判断只要进去的里面没有红包就把它隐藏掉,然后等新的红包发生过来。

?
1
2
3
4
5
6
7
8
9
10
11
12
# 删除第一个聊天框
def del_red_public(wait, driver):
    # 长按第一个聊天框
    r8 = wait.until(ec.element_to_be_clickable(
        (by.id, "com.tencent.mm:id/fzg")))
    touchaction(driver).long_press(r8).perform()
    time.sleep(1)
    # 点击长按后显示的删除
    wait.until(ec.element_to_be_clickable((by.xpath, "//android.widget.textview[@text='不显示该聊天']"))).click()
    # 点击弹出框的删除选项
    wait.until(ec.element_to_be_clickable(
        (by.id, "com.tencent.mm:id/ffp"))).click()

完整代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
from appium import webdriver
 
from selenium.webdriver.common.by import by
from selenium.webdriver.support.ui import webdriverwait
from appium.webdriver.common.touch_action import touchaction
from selenium.webdriver.support import expected_conditions as ec
import time
 
desired_caps = {
    "platformname": "android"# 系统
    "platformversion": "10.0"# 系统版本号
    "devicename": "b68548ed"# 设备名
    "apppackage": "com.tencent.mm"# 包名
    "appactivity": ".ui.launcherui"# app 启动时主 activity
    'unicodekeyboard': true,  # 使用自带输入法
    'noreset': true  # 保留 session 信息,可以避免重新登录
}
 
# 判断元素是否存在
 
def is_element_exist(driver, by, value):
    try:
        driver.find_element(by=by, value=value)
    except exception as e:
        return false
    else:
        return true
 
# 删除领取后的红包记录
 
 
def del_red_envelope(wait, driver):
    # 长按领取过的红包
    r8 = wait.until(ec.element_to_be_clickable(
        (by.id, "com.tencent.mm:id/ahs")))
    touchaction(driver).long_press(r8).perform()
    time.sleep(1)
    # 点击长按后显示的删除
    wait.until(ec.element_to_be_clickable(
        (by.id, "com.tencent.mm:id/dt5"))).click()
    # 点击弹出框的删除选项
    wait.until(ec.element_to_be_clickable(
        (by.id, "com.tencent.mm:id/ffp"))).click()
 
 
# 删除第一个聊天框
def del_red_public(wait, driver):
    # 长按第一个聊天框
    r8 = wait.until(ec.element_to_be_clickable(
        (by.id, "com.tencent.mm:id/fzg")))
    touchaction(driver).long_press(r8).perform()
    time.sleep(1)
    # 点击长按后显示的删除
    wait.until(ec.element_to_be_clickable((by.xpath, "//android.widget.textview[@text='不显示该聊天']"))).click()
    # 点击弹出框的删除选项
    wait.until(ec.element_to_be_clickable(
        (by.id, "com.tencent.mm:id/ffp"))).click()
 
 
if __name__ == '__main__':
    driver = webdriver.remote("http://localhost:4723/wd/hub", desired_caps)
    # 设置等待
    wait = webdriverwait(driver, 500)
 
    while true:
    # 进入第一个聊天窗口
        g73 = wait.until(ec.element_to_be_clickable(
            (by.id, "com.tencent.mm:id/fzg")))
        g73.click()
        print("进入了第一个聊天窗口")
        # 判断聊天窗是否是公众号
        is_weichat = is_element_exist(driver, "id", "com.tencent.mm:id/u1")
        if is_weichat == true:
        # while true:
            # 有红包则点击
            wait.until(ec.element_to_be_clickable(
                (by.id, "com.tencent.mm:id/u1"))).click()
            print("点击了红包")
            # 判断红包是否被领取
            is_open = is_element_exist(driver, "id", "com.tencent.mm:id/f4f")
            print("红包是否被领取:", is_open)
            if is_open == true:
                # 红包未被领取,点击开红包
                wait.until(ec.element_to_be_clickable(
                    (by.id, "com.tencent.mm:id/f4f"))).click()
                print('已经领取红包')
                # 返回群聊
                driver.keyevent(4)
                # 删除领取过的红包记录
                del_red_envelope(wait, driver)
                print('···删除已经领取的红包,等待新的红包')
                driver.keyevent(4)
            else:
                # 返回群聊
                driver.keyevent(4)
                # 删除领取过的红包记录
                del_red_envelope(wait, driver)
                print('···删除无法领取的红包,等待新的红包')
                driver.keyevent(4)
 
        else:
            print('没有红包则隐藏此聊天框')
            # 返回群聊
            driver.keyevent(4)
            # 删除第一个公众号窗口
            del_red_public(wait, driver)
            print('隐藏了第一个聊天框')

以上就是python+appium实现自动抢微信红包的详细内容,更多关于python 抢微信红包的资料请关注服务器之家其它相关文章!

原文链接:https://www.cnblogs.com/huny/p/14395348.html

延伸 · 阅读

精彩推荐