佳木斯湛栽影视文化发展公司

主頁 > 知識(shí)庫 > Python+Appium實(shí)現(xiàn)自動(dòng)搶微信紅包

Python+Appium實(shí)現(xiàn)自動(dòng)搶微信紅包

熱門標(biāo)簽:語音系統(tǒng) 客戶服務(wù) 百度AI接口 電話運(yùn)營中心 硅谷的囚徒呼叫中心 Win7旗艦版 呼叫中心市場(chǎng)需求 企業(yè)做大做強(qiáng)

環(huán)境準(zhǔn)備

  • appium環(huán)境
  • 安卓手機(jī)
  • usb數(shù)據(jù)線
  • python環(huán)境

實(shí)現(xiàn)思路

我們收到紅包和消息都是自動(dòng)置頂?shù)降谝粋€(gè),于是我們打開第一個(gè)判斷是否有紅包,沒有則隱藏此窗口。如果有則判斷紅包是否可以領(lǐng)取,如果有則領(lǐng)取紅包,否則刪除此紅包(不然會(huì)影響后面的判斷)
然后再進(jìn)行循環(huán)運(yùn)行和判斷。

code

首先看一下配置信息,因?yàn)槲沂褂玫檬钦鏅C(jī)小米9安卓10的系統(tǒng),代碼實(shí)現(xiàn)如下具體的信息填寫請(qǐng)根據(jù)自己的真實(shí)情況修改:

desired_caps = {
    "platformName": "Android",  # 系統(tǒng)
    "platformVersion": "10.0",  # 系統(tǒng)版本號(hào)
    "deviceName": "b68548ed",  # 設(shè)備名
    "appPackage": "com.tencent.mm",  # 包名
    "appActivity": ".ui.LauncherUI",  # app 啟動(dòng)時(shí)主 Activity
    'unicodeKeyboard': True,  # 使用自帶輸入法
    'noReset': True  # 保留 session 信息,可以避免重新登錄
}

因?yàn)辄c(diǎn)擊紅包后需要判斷點(diǎn)擊后的紅包是否被領(lǐng)取,即是否有開字,如圖所示:

所以我們定義一個(gè)判斷元素是否存在的方法,代碼實(shí)現(xiàn)如下:

def is_element_exist(driver, by, value):
    try:
        driver.find_element(by=by, value=value)
    except Exception as e:
        return False
    else:
        return True

因?yàn)榧t包無論是被自己領(lǐng)取還是被他人領(lǐng)取,之后都要?jiǎng)h除領(lǐng)取后的紅包記錄,所以我們?cè)賮矶x一個(gè)刪除已領(lǐng)取紅包的方法,代碼實(shí)現(xiàn)如下:

def del_red_envelope(wait, driver):
    # 長(zhǎng)按領(lǐng)取過的紅包
    r8 = wait.until(EC.element_to_be_clickable(
        (By.ID, "com.tencent.mm:id/ahs")))
    TouchAction(driver).long_press(r8).perform()
    time.sleep(1)
    # 點(diǎn)擊長(zhǎng)按后顯示的刪除
    wait.until(EC.element_to_be_clickable(
        (By.ID, "com.tencent.mm:id/dt5"))).click()
    # 點(diǎn)擊彈出框的刪除選項(xiàng)
    wait.until(EC.element_to_be_clickable(
        (By.ID, "com.tencent.mm:id/ffp"))).click()

同時(shí)有可能第一個(gè)是公眾號(hào)推送的消息,這樣會(huì)導(dǎo)致無法判斷,所以我們判斷只要進(jìn)去的里面沒有紅包就把它隱藏掉,然后等新的紅包發(fā)生過來。

# 刪除第一個(gè)聊天框
def del_red_public(wait, driver):
    # 長(zhǎng)按第一個(gè)聊天框
    r8 = wait.until(EC.element_to_be_clickable(
        (By.ID, "com.tencent.mm:id/fzg")))
    TouchAction(driver).long_press(r8).perform()
    time.sleep(1)
    # 點(diǎn)擊長(zhǎng)按后顯示的刪除
    wait.until(EC.element_to_be_clickable((By.XPATH, "http://android.widget.TextView[@text='不顯示該聊天']"))).click()
    # 點(diǎn)擊彈出框的刪除選項(xiàng)
    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",  # 系統(tǒng)
    "platformVersion": "10.0",  # 系統(tǒng)版本號(hào)
    "deviceName": "b68548ed",  # 設(shè)備名
    "appPackage": "com.tencent.mm",  # 包名
    "appActivity": ".ui.LauncherUI",  # app 啟動(dòng)時(shí)主 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

# 刪除領(lǐng)取后的紅包記錄


def del_red_envelope(wait, driver):
    # 長(zhǎng)按領(lǐng)取過的紅包
    r8 = wait.until(EC.element_to_be_clickable(
        (By.ID, "com.tencent.mm:id/ahs")))
    TouchAction(driver).long_press(r8).perform()
    time.sleep(1)
    # 點(diǎn)擊長(zhǎng)按后顯示的刪除
    wait.until(EC.element_to_be_clickable(
        (By.ID, "com.tencent.mm:id/dt5"))).click()
    # 點(diǎn)擊彈出框的刪除選項(xiàng)
    wait.until(EC.element_to_be_clickable(
        (By.ID, "com.tencent.mm:id/ffp"))).click()


# 刪除第一個(gè)聊天框
def del_red_public(wait, driver):
    # 長(zhǎng)按第一個(gè)聊天框
    r8 = wait.until(EC.element_to_be_clickable(
        (By.ID, "com.tencent.mm:id/fzg")))
    TouchAction(driver).long_press(r8).perform()
    time.sleep(1)
    # 點(diǎn)擊長(zhǎng)按后顯示的刪除
    wait.until(EC.element_to_be_clickable((By.XPATH, "http://android.widget.TextView[@text='不顯示該聊天']"))).click()
    # 點(diǎn)擊彈出框的刪除選項(xiàng)
    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)
    # 設(shè)置等待
    wait = WebDriverWait(driver, 500)

    while True:
    # 進(jìn)入第一個(gè)聊天窗口
        g73 = wait.until(EC.element_to_be_clickable(
            (By.ID, "com.tencent.mm:id/fzg")))
        g73.click()
        print("進(jìn)入了第一個(gè)聊天窗口")
        # 判斷聊天窗是否是公眾號(hào)
        is_weichat = is_element_exist(driver, "id", "com.tencent.mm:id/u1")
        if is_weichat == True:
        # while True:
            # 有紅包則點(diǎn)擊
            wait.until(EC.element_to_be_clickable(
                (By.ID, "com.tencent.mm:id/u1"))).click()
            print("點(diǎn)擊了紅包")
            # 判斷紅包是否被領(lǐng)取
            is_open = is_element_exist(driver, "id", "com.tencent.mm:id/f4f")
            print("紅包是否被領(lǐng)?。?, is_open)
            if is_open == True:
                # 紅包未被領(lǐng)取,點(diǎn)擊開紅包
                wait.until(EC.element_to_be_clickable(
                    (By.ID, "com.tencent.mm:id/f4f"))).click()
                print('已經(jīng)領(lǐng)取紅包')
                # 返回群聊
                driver.keyevent(4)
                # 刪除領(lǐng)取過的紅包記錄
                del_red_envelope(wait, driver)
                print('···刪除已經(jīng)領(lǐng)取的紅包,等待新的紅包')
                driver.keyevent(4)
            else:
                # 返回群聊
                driver.keyevent(4)
                # 刪除領(lǐng)取過的紅包記錄
                del_red_envelope(wait, driver)
                print('···刪除無法領(lǐng)取的紅包,等待新的紅包')
                driver.keyevent(4)

        else:
            print('沒有紅包則隱藏此聊天框')
            # 返回群聊
            driver.keyevent(4)
            # 刪除第一個(gè)公眾號(hào)窗口
            del_red_public(wait, driver)
            print('隱藏了第一個(gè)聊天框')

以上就是Python+Appium實(shí)現(xiàn)自動(dòng)搶微信紅包的詳細(xì)內(nèi)容,更多關(guān)于Python 搶微信紅包的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

您可能感興趣的文章:
  • Python模擬登錄網(wǎng)易云音樂并自動(dòng)簽到
  • Python爬蟲之自動(dòng)爬取某車之家各車銷售數(shù)據(jù)
  • Python一行代碼實(shí)現(xiàn)自動(dòng)發(fā)郵件功能
  • 使用Gitee自動(dòng)化部署python腳本的詳細(xì)過程
  • Python實(shí)現(xiàn)網(wǎng)絡(luò)自動(dòng)化eNSP
  • python 辦公自動(dòng)化——基于pyqt5和openpyxl統(tǒng)計(jì)符合要求的名單
  • 瀏覽器常用基本操作之python3+selenium4自動(dòng)化測(cè)試(基礎(chǔ)篇3)
  • Python自動(dòng)化之定位方法大殺器xpath
  • 還在手動(dòng)蓋樓抽獎(jiǎng)?教你用Python實(shí)現(xiàn)自動(dòng)評(píng)論蓋樓抽獎(jiǎng)(一)

標(biāo)簽:喀什 濟(jì)南 安康 海南 崇左 山西 山西 長(zhǎng)沙

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Python+Appium實(shí)現(xiàn)自動(dòng)搶微信紅包》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    溆浦县| 锦屏县| 星子县| 保康县| 曲松县| 泸水县| 张掖市| 沈阳市| 洛隆县| 邵武市| 图片| 堆龙德庆县| 香河县| 康定县| 东辽县| 宁海县| 镇康县| 垣曲县| 龙山县| 淮北市| 浑源县| 灵璧县| 明光市| 潍坊市| 沐川县| 广宁县| 昌宁县| 吉安市| 亳州市| 塔河县| 赤峰市| 班玛县| 奎屯市| 南涧| 中方县| 衡水市| 长兴县| 余江县| 泸西县| 屯门区| 宜春市|