为了增加我在微信好友的存在感,然后呢,我就隔两三天给开通微信运动的好友dian👍。
但是呢,每次点,都特别浪费时间,像我这上千人的微信运动好友,点的手都疼了,因此呢,我绝对好好研究一下,怎么解放双手,实现无人值守操作。
思来想去,突然来了灵感,之前用过的一款测试的UI架构 uiautomator2 ,是基于Python的架构,也是挺好使的,然后就试着码代码,搞了一下,原理是通过模拟屏幕点击。
代码已经放出,需要解放双手的小伙伴可以试一下。
测试的话,记得开启手机调试和模拟点击的权限。程序运行比较耗时,不要着急,慢慢等待即可。
#!/use/bin/evn python
# -*- coding: utf-8 -*-
# project: pythonProject wx_sports_hearts
# author: honey
# date: 2021/10/24
"""
使用说明:
测试环境:小米手机8, python3.8 , mac系统,微信 8.0.15 版本
1.安装:
pip install --pre uiautomator2
pip install weditor
2.初始化客户端和服务端
a.通过数据线连接手机
b.手机开启USB调试,模拟点击,允许USB安装
c.执行下面的初始化操作
python -m uiautomator2 init
3.打开web界面,该界面主要是为了获取控件的ID 【自己开发调试使用】
python -m weditor
4.打开微信,点击微信运动,点击步数排行榜,然后开始运行该程序即可。
"""
import uiautomator2 as u2
from uiautomator2.exceptions import InjectPermissionError
d = u2.connect()
d.implicitly_wait(0.2)
rid = '//*[@resource-id="com.tencent.mm:id/c68"]'
LLayout = '/android.widget.LinearLayout'
RLayout = '/android.widget.RelativeLayout'
TView = '/android.view.View'
rid_RLayout = rid + RLayout
LLayout1_RLayout2 = LLayout + "[1]" + RLayout + "[2]"
LLayout1_RLayout1 = LLayout + "[1]" + RLayout + "[1]"
Text_RLayout = RLayout + "[1]" + TView + "[1]"
i = 1
position = []
ele_index_list = []
mine_name = d.xpath(rid_RLayout + '[2]' + LLayout1_RLayout2 + RLayout + "[1]" + Text_RLayout).get_text()
print('当前用户昵称:', mine_name)
# 黑名单,也就是通过昵称跳过赞
black_name_list = [
mine_name,
'赵二狗'
]
love_count = 0
while i < 20:
try:
ele_path = d.xpath(rid_RLayout + "[" + str(i) + "]" + LLayout1_RLayout2 + RLayout + "[2]")
ele_name = d.xpath(
rid_RLayout + "[" + str(i) + "]" + LLayout1_RLayout2 + RLayout + "[1]" + Text_RLayout).get_text()
ele_index = d.xpath(
rid_RLayout + "[" + str(i) + "]" + LLayout1_RLayout1 + RLayout + "[1]/android.widget.TextView").get_text()
x, y = ele_path.center()
if i == 1:
position = [[x, y], [x, y]]
else:
if len(position) == 0:
position = [[x, y], [x, y]]
position[1] = [x, y]
if ele_path.exists:
if ele_index in ele_index_list:
continue
ele_index_list.append(ele_index)
if ele_name in black_name_list:
continue
ele_path.click()
love_count += 1
print(ele_name, '微信运动已赞')
except InjectPermissionError as e:
print(e)
exit(0)
except Exception:
if len(position) == 2 and position[0][1] != position[1][1]:
try:
if d(resourceId="com.tencent.mm:id/dzn").get_text() == '邀请朋友':
break
except Exception:
pass
print('当前页面已经赞完成,继续下一页')
d.drag(position[1][0], position[1][1], position[0][0], position[0][1])
i = 0
position = []
finally:
i += 1
print('点赞结束', love_count)
眨眼中,红心赞统统完成,太棒喽