Python+Appium實現(xiàn)自動搶微信紅包
環(huán)境準備
- appium環(huán)境
- 安卓手機
- usb數(shù)據(jù)線
- python環(huán)境
實現(xiàn)思路
我們收到紅包和消息都是自動置頂?shù)降谝粋€,于是我們打開第一個判斷是否有紅包,沒有則隱藏此窗口。如果有則判斷紅包是否可以領(lǐng)取,如果有則領(lǐng)取紅包,否則刪除此紅包(不然會影響后面的判斷)
然后再進行循環(huán)運行和判斷。
code
首先看一下配置信息,因為我使用得是真機小米9安卓10的系統(tǒng),代碼實現(xiàn)如下具體的信息填寫請根據(jù)自己的真實情況修改:
desired_caps = { "platformName": "Android", # 系統(tǒng) "platformVersion": "10.0", # 系統(tǒng)版本號 "deviceName": "b68548ed", # 設(shè)備名 "appPackage": "com.tencent.mm", # 包名 "appActivity": ".ui.LauncherUI", # app 啟動時主 Activity 'unicodeKeyboard': True, # 使用自帶輸入法 'noReset': True # 保留 session 信息,可以避免重新登錄 }
因為點擊紅包后需要判斷點擊后的紅包是否被領(lǐng)取,即是否有開字,如圖所示:
所以我們定義一個判斷元素是否存在的方法,代碼實現(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
因為紅包無論是被自己領(lǐng)取還是被他人領(lǐng)取,之后都要刪除領(lǐng)取后的紅包記錄,所以我們再來定義一個刪除已領(lǐng)取紅包的方法,代碼實現(xiàn)如下:
def del_red_envelope(wait, driver): # 長按領(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) # 點擊長按后顯示的刪除 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()
同時有可能第一個是公眾號推送的消息,這樣會導(dǎo)致無法判斷,所以我們判斷只要進去的里面沒有紅包就把它隱藏掉,然后等新的紅包發(fā)生過來。
# 刪除第一個聊天框 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, "http://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", # 系統(tǒng) "platformVersion": "10.0", # 系統(tǒng)版本號 "deviceName": "b68548ed", # 設(shè)備名 "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 # 刪除領(lǐng)取后的紅包記錄 def del_red_envelope(wait, driver): # 長按領(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) # 點擊長按后顯示的刪除 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, "http://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) # 設(shè)置等待 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("點擊了紅包") # 判斷紅包是否被領(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)取,點擊開紅包 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) # 刪除第一個公眾號窗口 del_red_public(wait, driver) print('隱藏了第一個聊天框')
以上就是Python+Appium實現(xiàn)自動搶微信紅包的詳細內(nèi)容,更多關(guān)于Python 搶微信紅包的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
學(xué)習(xí)python中matplotlib繪圖設(shè)置坐標軸刻度、文本
本篇文章給大家詳細介紹了python中matplotlib繪圖設(shè)置坐標軸刻度、文本等基本知識點,對此有興趣的朋友學(xué)習(xí)下吧。2018-02-02python GUI庫圖形界面開發(fā)之PyQt5復(fù)選框控件QCheckBox詳細使用方法與實例
這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5復(fù)選框控件QCheckBox詳細使用方法與實例,需要的朋友可以參考下2020-02-02python生成tensorflow輸入輸出的圖像格式的方法
本篇文章主要介紹了python生成tensorflow輸入輸出的圖像格式的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02淺談python3 構(gòu)造函數(shù)和析構(gòu)函數(shù)
這篇文章主要介紹了淺談python3 構(gòu)造函數(shù)和析構(gòu)函數(shù),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03python用pyinstaller封裝exe雙擊后瘋狂閃退解決辦法
本文主要介紹了python用pyinstaller封裝exe雙擊后瘋狂閃退解決辦法,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11