Python读书笔记(python学习笔记学以致用)
Python读书笔记(python学习笔记学以致用)检查是否安装成功高级系统设置系统环境变量系统环境变量 path 修改 前面刚添加到系统变量的 android 添加到最底部 %android%
使用python和adb驱动来操作手机看完了python的语法,能做什么呢,今天把以前的语法都复习一遍,看看怎么把各种语法知识组合起来解决问题。 今天就来尝试着把前面看过的基础语法知识组合起来尝试使用python来操作手机,进行默认人的滑动 点击 返回等操作!
准备工作通过度娘查找adb命令的用法 准备好adb驱动文件,和下载好模拟器 adb驱动:链接:https://pan.baidu.com/s/1_Td_HSkU5m-S-Go1Vm6lVg 提取码:iaog 模拟器使用的是网易的mumu模拟器。
安装adb驱动一路下一步晚装完毕后,还需要把adb添加到系统环境变量
    
此时电脑右键属性

属性

高级系统设置

系统环境变量

系统环境变量 path 修改 前面刚添加到系统变量的 android 添加到最底部 %android%

检查是否安装成功
执行过程- 连接模拟器
 - 打开要操作的app
 - 循环 随机点击页面打开具体页面
 - 随机向上滑动模拟翻页,并延迟
 - 返回到列表页 整个的执行过程大概如上coding:utf-8import osimport randomimport time
 
#封装一个函数用来执行adb命令 并返回执行的结果
def ExecuteCmd(cmd):
    print(cmd)
    return  os.popen(cmd)
#函数入口
if __name__ == '__main__':
#使用adb连接模拟器
    connect =ExecuteCmd("adb connect 127.0.0.1:7555")
# 顶一个布尔型变量,赋值为真,下面使用循环要用到
    WatchNews=True
    # 输出上面使用adb连接模拟器后的返回值
    print("返回结果:" connect.read())
#使用if 语句判断返回的语句里是否包涵了 already,如果包涵了就表示连接模拟器成功
    if  str(connect.read()) in "already":
        while WatchNews:
         
            #定义各一个变量y1 用来保存要滑动的y轴坐标,步长为55
            y1 = random.randrange(268 700 55)
            # 使用tap命令 发送点击命令
            connect= ExecuteCmd("adb shell input tap  {x} {y}".format(x=random.randint(30 300) y=int(y1)))
            slide = random.randint(300 900)
            #顶一个变量用来保存随机往下翻页的次数,用来模拟翻页
            num=0
          #随机向上滑动1-20次
            for UpSlide in range(1 int(random.randint(1 20))):
                num  = 1
                #发送滑动命令
                connect= ExecuteCmd("adb shell input swipe 220 330 {x} {y}".format(x=random.randint(180 300) y=int(random.randrange(1 300 150))))
                second=random.randint(1 5)
                #延迟函数
                time.sleep(second)
                print("正在执行第:{x}次,延迟{y}毫秒 返回值{z}".format(x = num y=second z=connect.read()))
            #返回到主页
            connect= ExecuteCmd("adb shell input keyevent  4" )
           # connect= ExecuteCmd("adb shell input swipe 150 330 {x} {y}".format(x=random.randint(180 400) y=random.randint(180 400)))
        time.sleep(random.randint(1 5))          




