如何用Python+Appium实现自动抢微信红包
发表于:2024-10-14 作者:千家信息网编辑
千家信息网最后更新 2024年10月14日,本文小编为大家详细介绍"如何用Python+Appium实现自动抢微信红包",内容详细,步骤清晰,细节处理妥当,希望这篇"如何用Python+Appium实现自动抢微信红包"文章能帮助大家解决疑惑,下
千家信息网最后更新 2024年10月14日如何用Python+Appium实现自动抢微信红包实现思路
本文小编为大家详细介绍"如何用Python+Appium实现自动抢微信红包",内容详细,步骤清晰,细节处理妥当,希望这篇"如何用Python+Appium实现自动抢微信红包"文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
环境准备
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 webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom appium.webdriver.common.touch_action import TouchActionfrom selenium.webdriver.support import expected_conditions as ECimport timedesired_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实现自动抢微信红包"文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注行业资讯频道。
红包
系统
代码
信息
公众
文章
环境
元素
内容
思路
方法
消息
版本
设备
输入法
安卓
登录
输入
妥当
同时
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
稳健安全的rds数据库
数据库增删改查c
企业员工网络安全管理规定
网络安全网站政策
计算机网络技术自学书
网络安全国教育的手抄报
扎实做好重要时点网络安全
数据库系统及其实现
电竞服务器报价
迈克菲网络安全扫描报告在哪
网络安全教育 继续教育
中专计算机网络技术老师评语
上海弗朗斯软件开发有限公司
高澜液冷服务器
梁溪区重型软件开发规范
网络安全小报电子图片
网络安全事件应急预案以及处置
网络安全法 二审
数据库RAC配置需求
上海网络安全培训 百姓网
自研服务器应用开发项目
苏州云至胜软件开发有限公司
网络安全监察规定
硬件网络技术支持设备
庆夕网络技术工作室
河北第三方软件开发定制
大唐无双服务器副本完成
数据库分析DNA序列的GC含量
杭州苹果软件开发怎么选
数据挖掘与网络安全态势感知