一提及自动化技术,很有可能大伙儿想起的是 App 端 Appium、Airtest、AutoJS,亦或是 Selenium、Puppeteer、Cypress 等 Web 端自动化技术架构 这篇文章内容,我将和大伙儿聊一聊 PC 端自动化技术专用工具 - WinAppDriver

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  打开「 开发者模式 」

关键词搜索「 开发人员设定 」,挑选打开「 开发者模式 」

image

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」原素操纵树值,为此来协助大家开展迅速精准定位原素,从而健全自动化技术脚本制作

如果你觉得文章内容还不错,请大伙儿 关注点赞、共享、留言板留言 下,由于这将就是我不断輸出大量高品质文章内容的最強驱动力!

共享 Python 自动化技术及网络爬虫、数据统计分析实战演练干货知识,热烈欢迎关心。

评论(0条)

刀客源码 游客评论