Python+Appium實現自動搶微信紅包


前言

過年的時候總是少不了紅包,不知從何時開始微信紅包橫空出世,對於網速和手速慢的人只能在一旁觀望,做為python的學習者就是要運用編程解決生活和工作上的事情。
於是我用python解決我們的手速問題python實現自動搶微信紅包,至於網速慢得那就只能自己花錢提升了。

環境准備

appium環境
安卓手機
usb數據線
python環境

實現思路

我們收到紅包和消息都是自動置頂到第一個,於是我們打開第一個判斷是否有紅包,沒有則隱藏此窗口。如果有則判斷紅包是否可以領取,如果有則領取紅包,否則刪除此紅包(不然會影響后面的判斷)
然后再進行循環運行和判斷。

CODE

首先看一下配置信息,因為我使用得是真機小米9安卓10的系統,代碼實現如下具體的信息填寫請根據自己的真實情況修改:

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()

完整代碼如下:

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('隱藏了第一個聊天框')

此程序可能會清空你得微信聊天對話窗口,對內容消息敏感得請謹慎運行。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM