聊聊 PC 端自动化最佳方案 - WinAppDriver

来源:AirPython

1. 前言

一提到自动化,可能大家想到的是 App 端的 Appium、Airtest、AutoJS,亦或是 Selenium、Puppeteer、Cypress 等 Web 端的自动化框架

本篇文章,将和大家聊聊 PC 端的自动化工具 - WinAppDriver

2. 准备

WinAppDriver,全称为 Windows Application Driver,它是 Windows 上一个类似 Selenium 的 UI 自动化驱动服务框架

它支持 Appium,可以使用 Appium-Python-Client 依赖库完成对 Windows 桌面程序的自动化操作

项目地址:https://github.com/Microsoft/WinAppDriver

需要注意的是,要使用 WinAppDriver 服务框架完成 Windows 的自动化,需要满足 Windows10 或 Windows Server 2016 以上系统

另外,它支持的应用程序包含:

  • UWP  -  Universal Windows Platform

  • WinForms  -  Windows Forms

  • WPF  -  Windows Presentation Foundation

  • Win32  -  Classic Windows

在实现之前,我们需要做好以下准备工作

2-1  开启「 开发者模式 」

关键字搜索「 开发者设置 」,选择开启「 开发者模式 」

3d25fc2f0bf87f16ea70776d28fbded9.png

2-2  安装窗口组件元素识别工具

常用的 2 种窗口元素识别工具为:inspect.exe、FlaUInspect

其中

作为官方的组件元素识别工具,inspect.exe 集成于 Windows SDK

如果本地不存在该文件,可以通过下面链接进行安装

https://download.microsoft.com/download/4/d/2/4d2b7011-606a-467e-99b4-99550bf24ffc/windowssdk/winsdksetup.exe

相比 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

3. 实战一下

我们以操作 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

# 模拟屏幕滑动
# 1、移动到某个元素区域
ActionChains(self.weixin_driver).move_to_element(
     self.weixin_driver.find_element_by_name("element_name")).perform()

# 2、滑动界面
# 比如,向上滚动,模拟滑动
win32api.mouse_event(win32con.MOUSEEVENTF_WHEEL, 0, 0, -500)

完成自动化操作后,就可以主动释放资源、关闭 WinAppDriver 服务

# 释放资源及关闭服务
def tearDownFunc(self):
    print("准备退出")
    sleep(2)

    # 1、释放资源
    self.weixin_driver.quit()

    # 2、关闭WinAppDriver应用程序
    os.system(' @taskkill /f /im WinAppDriver.exe')

4. 最后

在实际使用过程中,可能会遇到复杂的桌面应用程序,这时我们可以通过打印驱动对象的「 page_source」元素控制树值,以此来帮助我们进行快速定位元素,进而完善自动化脚本

-------- End --------

a4bcb7cf3af69981b59124ae8f12709b.png
精选资料

回复关键词,获取对应的资料:

关键词资料名称
600《Python知识手册》
md《Markdown速查表》
time《Python时间使用指南》
str《Python字符串速查表》
pip《Python:Pip速查表》
style《Pandas表格样式配置指南》
mat《Matplotlib入门100个案例》
px《Plotly Express可视化指南》
精选视频

可视化: Plotly Express

财经: Plotly在投资领域的应用 | 绘制K线图表

排序算法: 汇总 | 冒泡排序 | 选择排序 | 快速排序 | 归并排序 | 堆排序 | 插入排序 | 希尔排序 | 计数排序 | 桶排序 | 基数排序

6dfc6901e3e94e61cd5a5c371c0463a0.png
  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自动化环境搭建 1、Python(推荐使用ActivePython,这个版本PATH已经配好了,也安了一些像pip这样的包) 2、WxPython(安装,保持勾选状态,不要把勾去掉!!!) 3、安装rf,以管理员身份运行cmd, pip install robotframework 4、安装RIDE,以管理员身份运行cmd, pip install robotframework-ride 5、安装Selenium2Library,以管理员身份运行cmd,pip install robotframework-selenium2library 6、安装python32位拓展,以管理员身份运行cmd,pip install pywin32 7、安装DatabaseLibrary , 以管理员身份运行cmd, pip install robotframework-databaselibrary 安装VCForPython27 安装vcredist_x64 安装驱动MySQL-python-1.2.3.win-amd64-py2.7(根据电脑自行选择32,64) 8、pip install pyodbc 需要更新setuptools,运行pip install --upgrade setuptools pip(第一遍报错,在执行一遍命令即可) 然后安装连接pyodbc的驱动mysql-connector-odbc-5.3.9-winx64.msi。(安装到这一步就完成了。下面是测试步骤,所有安装完成后再测试也可以)安装完成后打开   打开数据源添加驱动(添加按钮)   填写完整后点击test返回successful表示成功   选择库 打开robot新建测试用例输入如下内容   Connect To Database Using Custom Params 连接数据库的关键字 pyodbc 连接数据库的工具 database='test', 数据库的名字 user='root', 连接数据库的用户名 password='', 密 码 host='127.0.0.1', 数据库的IP port=3306, 数据库的口号 Driver='{MySQL ODBC 5.3 Unicode Driver}' 数据库的驱动 query 查询语句 Disconnect From Database 断开连接数据库   Execute Sql String     执行sql语句 9、安装AutoItLibrary库(模拟键盘鼠标操作) 安装(选择x64安装) (路径改一下,因为program files(x86)路径win系统似乎会禁止读写,改成c:\AutoIt3即可) 安装 解压后,使用cmd(管理员启动),cd到包目录执行python setup.py install命令安装 9、pip freeze检查安装包 启动ride: 命令进入ride: 1:cmd; 2:cd c:\; 3:cd Python27; 4:cd Scripts; 5:输入命令:python ride.py。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值