你们好,我是安果!html
一提到自动化,可能你们想到的是 App 端的 Appium、Airtest、AutoJS,亦或是 Selenium、Puppeteer、Cypress 等 Web 端的自动化框架git
本篇文章,我将和你们聊聊 PC 端的自动化工具 - WinAppDrivergithub
WinAppDriver,全称为 Windows Application Driver,它是 Windows 上一个相似 Selenium 的 UI 自动化驱动服务框架web
它支持 Appium,可使用 Appium-Python-Client 依赖库完成对 Windows 桌面程序的自动化操做windows
项目地址:https://github.com/Microsoft/WinAppDriverapi
须要注意的是,要使用 WinAppDriver 服务框架完成 Windows 的自动化,须要知足 Windows10 或 Windows Server 2016 以上系统微信
另外,它支持的应用程序包含:app
UWP - Universal Windows Platform框架
WinForms - Windows Forms工具
WPF - Windows Presentation Foundation
Win32 - Classic Windows
在实现以前,咱们须要作好如下准备工做
2-1 开启「 开发者模式 」
关键字搜索「 开发者设置 」,选择开启「 开发者模式 」
2-2 安装窗口组件元素识别工具
经常使用的 2 种窗口元素识别工具为:inspect.exe、FlaUInspect
其中
做为官方的组件元素识别工具,inspect.exe 集成于 Windows SDK
若是本地不存在该文件,能够经过下面连接进行安装
相比 inspect.exe,FlaUInspect 界面更简洁,功能更易用( 推荐 )
项目地址:https://github.com/FlaUI/FlaUInspect
2-3 安装 WinAppDriver
经过下面连接下载 WinAppDriver 应用程序,并在本地运行起来
https://github.com/Microsoft/WinAppDriver/releases
2-4 搭建 Appium 环境
这部份内容涉及 NodeJS 安装及 Appium-Server 环境的搭建
能够参考:https://www.cnblogs.com/amoyshmily/p/10500687.html
2-5 安装依赖
最后安装 Python 依赖库 Appium-Python-Client
# 安装依赖 Appium-Python-Client pip3 install Appium-Python-Client
咱们以操做 PC 端的微信为例,聊聊自动化的常见步骤
首先,咱们在本机打开 WinAppDriver 服务,让它在后台运行
而后,咱们使用 Python 编写自动化脚本
经过 ip 地址、端口号及 PC 版微信的绝对路径,使用 Appium 打开微信
import time, os from appium import webdriver from selenium.webdriver import ActionChains from selenium.webdriver.common.keys import Keys from time import sleep class Auto(): def open_weixin(self, host='localhost', port=4723): # 打开WinAppDriver服务 # 注意:若是手动开启,则能够注释掉 # os.system(r'start "" /d "C:\Program Files\Windows Application Driver\" "WinAppDriver.exe"') # 配置信息 # 包含:平台名、系统、应用程序绝对路径 desired_caps = {'platformName': 'Windows', 'deviceName': 'WindowsPC', 'app': r"D:\Program Files (x86)\Tencent\WeChat\WeChat.exe"} try: # 链接WinAppDriver服务,打开目标软件 self.driver = webdriver.Remote('http://{}:{}'.format(host, port), desired_caps) except Exception as e: raise AssertionError(e)
接着,经过「 组件元素识别工具 」拿到界面元素的属性值,执行常见的点击、移动、滑动等操做
好比:点击「 文件传输助手 」,发送一条信息
# 给文件传输助手发送一条信息 def send_msg(self, element_name, msg): """ :param element_name:元素name值 :param msg: :return: """ # 经过name属性,找到目标元素 chat_element = self.weixin_driver.find_element_by_name(target_name) # 点击元素,进入聊天界面 chat_element.click() # 找到输入框,并输入 self.weixin_driver.find_element_by_name("输入").send_keys(msg) # 点击右下角的发送,发送消息出去 self.weixin_driver.find_element_by_name("发送(S)").click()
须要注意的是,若是涉及界面的滑动,可使用「 ActionChains 」移动鼠标,而后使用 win32api 和 win32con 模拟屏幕滑动便可
import win32api import win32con from appium import webdriver from selenium.webdriver import ActionChains # 模拟屏幕滑动 # 一、移动到某个元素区域 ActionChains(self.weixin_driver).move_to_element( self.weixin_driver.find_element_by_name("element_name")).perform() # 二、滑动界面 # 好比,向上滚动,模拟滑动 win32api.mouse_event(win32con.MOUSEEVENTF_WHEEL, 0, 0, -500)
完成自动化操做后,就能够主动释放资源、关闭 WinAppDriver 服务
# 释放资源及关闭服务 def tearDownFunc(self): print("准备退出") sleep(2) # 一、释放资源 self.weixin_driver.quit() # 二、关闭WinAppDriver应用程序 os.system(' @taskkill /f /im WinAppDriver.exe')
在实际使用过程当中,可能会遇到复杂的桌面应用程序,这时咱们能够经过打印驱动对象的「 page_source」元素控制树值,以此来帮助咱们进行快速定位元素,进而完善自动化脚本
若是你以为文章还不错,请你们 点赞、分享、留言 下,由于这将是我持续输出更多优质文章的最强动力!