目录

 

硬件准备:

功能实现流程:

手机与笔记本连接:

安装ADB:

安装Appium Desktop:

使用Appium Desktop获取元素信息:

安装AndroidViewClient

完整代码


硬件准备

笔记本:win10、python2.7

手机:HUAWEI Mate 20 Pro

USB数据线

功能实现流程

  1. 手机与笔记本连接
  2. 安装ADB,实现笔记本与手机的交互
  3. 安装Appium,获取手机界面组件的ID等信息
  4. 安装AndroidViewClient
  5. 编写脚本

手机与笔记本连接

参考:https://jingyan.baidu.com/article/5552ef47af9c47518ffbc9a6.html

注意:HUAWEI Mate 20 Pro 有个坑,执行上述操作前需要把悬浮球关闭,否则会连接不成功。

关闭悬浮球位置:手机设置--系统和更新--系统导航方式--悬浮导航

安装ADB

adb即Android Debug Bridge,可以实现在pc端通过cmd命令行调试安卓移动端。

adb下载安装参考:https://www.jianshu.com/p/7c0a6da594c8 验证:win+R打开命令行,输入adb devices显示如下图表示安装成功(前提是手机与笔记本已正确连接)

python微信朋友圈点赞 python微信朋友圈自动点赞_朋友圈

ADB相关命令:

1.显示当前运行的全部模拟器:    adb devices
2.启动ADB    adb start-server
3.停止ADB   adb kill-server

4.点击屏幕坐标为 X Y的位置:adb shell input tap X Y

5.滑动屏幕:adb shell input swipe startX startY endX endY

验证:在cmd窗口输入 adb shell input swipe 800 800 500 500

正常情况下手机屏幕会向上滑动一段距离,如果没有,需要排查前面的步骤是否有操作失误。

安装Appium Desktop

我们用Appium desktop来获取元素的基本信息。

下载地址:https://github.com/appium/appium-desktop/releases

双击.EXE文件安装即可

使用Appium Desktop获取元素信息

  1. 打开appium desktop

python微信朋友圈点赞 python微信朋友圈自动点赞_朋友圈_02

python微信朋友圈点赞 python微信朋友圈自动点赞_python_03

python微信朋友圈点赞 python微信朋友圈自动点赞_python微信朋友圈点赞_04

输入下面的信息,点击启动会话

{
  "deviceName": "HUAWEI Mate 20 Pro (UD)",
  "automationName": "Appium",
  "platformName": "Android",
  "platformVersion": "10",
  "appPackage": "com.tencent.mm",
  "appActivity": "com.tencent.mm.ui.LauncherUI",
  "noReset": "True",
  "noSign": "True"
}

点击启动会话后,手机会提示安装apk,点击安装即可。之后电脑会显示下面界面

python微信朋友圈点赞 python微信朋友圈自动点赞_点赞_05

此时可以借助appium desktop来获得元素的id,下图我们得到评论按钮的id是:com.tencent.mm:id/eyz

python微信朋友圈点赞 python微信朋友圈自动点赞_微信_06

安装AndroidViewClient

pip install AndroidViewClient

注:AndroidViewClient不支持python3.X,安装之前要确保你的python版本是python2.7

完整代码

# coding: utf-8
import sys
import os
import re
import time
from com.dtmilano.android.viewclient import ViewClient
def wechat():
    # 连接手机
    device, serialno = ViewClient.connectToDeviceOrExit()
    vc = ViewClient(device, serialno)
    # 找到评论按钮
    # 使用appium desktop 来查看评论按钮的resource-id为"com.tencent.mm:id/eyz"
    # 输入框是com.tencent.mm:id/aqe,发送按钮是aql;

    succeed = 0
    Failure = 0
    while True:
        try:
            # 找到评论按钮并点击一下
            vc.dump()
            pinglun_button = vc.findViewByIdOrRaise("com.tencent.mm:id/eyz")
            pinglun_button.touch()

            # 找到点赞按钮,并点击
            vc.dump()
            zan_button = vc.findViewWithText(u"赞")
            zan_button.touch()
            succeed = succeed + 1
            print "已为%d人点赞" %(succeed)
        except:
            #失败的可能是已经点过赞了
            Failure = Failure + 1
            print "失败了%d次" %(Failure)
        #有时候会误点击到拍照按钮,所以通过一个滑动先将界面恢复到正常状态
        os.system("adb shell input swipe 900 900 900 850")
        os.system("adb shell input swipe 900 900 900 500")
if __name__ == '__main__':
    wechat()

注意:运行代码前要吧appium desktop退出