Python Appium實現自動搶微信紅包

2022-01-10 18:35:52 字數 4524 閱讀 1657

我們收到紅包和訊息都是自動置頂到第乙個,於是我們開啟第乙個判斷是否有紅包,沒有則隱藏此視窗。如果有則判斷紅包是否可以領取,如果有則領取紅包,否則刪除此紅包(不然會影響後面的判斷)

然後再進行迴圈執行和判斷。

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

desired_caps =
因為點選紅包後需要判斷點選後的紅包是否被領取,即是否有開字,如圖所示:

所以我們定義乙個判斷元素是否存在的方法,**實現如下:

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

# 點選彈出框的刪除選項

# 刪除第乙個聊天框

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, "'不顯示該聊天']"))).click()

# 點選彈出框的刪除選項

wait.until(ec.element_to_be_clickable(

(by.id, "com.tencent.mm:id/ffp"))).click()

完整**如下:

from selenium.webdriver.common.by import by

from selenium.webdriver.support.ui import webdriverwait

from selenium.webdriver.support import expected_conditions as ec

import time

desired_caps =

# 判斷元素是否存在

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, "'不顯示該聊天']"))).click()

# 點選彈出框的刪除選項

# 設定等待

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實現企業微信自動打卡

import time def init self,desired caps,image name self.url self.driver webdriver.remote self.url,desired caps self.driver.implicitly wait 10 self.desi...

Python Appium 實現上下左右滑動螢幕

實現較為累贅,可以把獲取螢幕size,width,height 屬性作為公共屬性單抽出來,不過不影響使用 還有可以優化的地方,try except 沒有起到我預期的效果,暫時先留著 import time from mocirepatinet import phoneset import trace...

Python Appium實現滑動引導頁進入APP

1 建立session 2 運用 模擬手指從右向左滑動的操作 獲取螢幕寬度和高度 def getsize self x self.driver.get window size width y self.driver.get window size height return x,y 向左滑動 def...