windows 桌面应用自动化-Uiautomator

python 对windows 应用进行UI自动化
环境&工具: Python3, inspect
inspect下载&安装使用:
下载 Windows SDK
启动: Inspect.exe 位于 SDK 安装路径的 \bin<version><platform> 文件夹中,如我的路径:D:\inspect\bin\10.0.22621.0\x64\inspect.exe
使用方法:
我的需求为设置码流卡,便以此为例。
首先获取窗口,启动应用后点击inspect中该应用的标题:
在这里插入图片描述

		auto.uiautomation.DEBUG_SEARCH_TIME =True
        # 设置全局搜索超时时间
        auto.uiautomation.SetGlobalSearchTimeout(2)
        # 码流卡窗口
        XpressWindow = auto.WindowControl(searchDepth=1, Name='DekTec StreamXpress - Stream Player', desc='码流卡窗口')
        if not self.XpressWindow.Exists():
            # 设置窗口前置
            subprocess.Popen('StreamXpress')# 需将StreatmXpress.exe应用所在文件夹配置环境变量
            XpressWindow = auto.WindowControl(searchDepth=1, Name='DekTec StreamXpress - Stream Player', desc='码流卡窗口')
        # 激活窗口
        XpressWindow.SetActive()
        # 设置为顶层
        XpressWindow.SetTopmost(True)
        

再对窗口中的元素进行Click操作
点击选择框
点击所需选项

		XpressWindow.ComboBoxControl(AutomationId="2372", desc="Modulation").Click(waitTime=0.01)
        sleep(1)
        XpressWindow.ListItemControl(Name=" DVB-C").Click(waitTime=0.01)
        sleep(1)

但是该码流卡在实际选择对应文件后窗口名会变,上诉代码就会报错:找不到DekTec StreamXpress - Stream Player窗口
在这里插入图片描述
因此想了一个办法:将该应用固定在任务栏,先从任务栏启动该应用,再获取该应用的窗口名
首先从任务栏一层一层定位该应用,根据inspect的获取Name或AutomationId 进行定位,最后Click启动
在这里插入图片描述

		# 从任务栏获取码流卡窗口
        task = auto.PaneControl(searchDepth=1, Name="任务栏").PaneControl(Name="DesktopWindowXamlSource").\
            PaneControl(Class="Windows.UI.Input.InputSite.WindowClass")\
            .PaneControl(AutomationId="TaskbarFrame").GroupControl(AutomationId="TaskbarFrameRepeater")

        task.ButtonControl(searchDepth=1, AutomationId="{7C5A40EF-A0FB-4BFC-874A-C0F2E0B9FA8E}\DekTec\StreamXpress\StreamXpress.exe").Click(waitTime=0.01)

启动后获取当前窗口的名字

		# 获取窗口句柄
        hwnd = win32gui.GetForegroundWindow()
        # 获取窗口标题
        currentWindow = win32gui.GetWindowText(hwnd)
        return currentWindow

现在就可以获取窗口,并进行所需操作了。

        XpressWindow = auto.WindowControl(searchDepth=1, Name=currentWindow, desc='DekTec StreamXpress - Stream Player对话框')

完整代码

import os
from sys import argv

import uiautomation as auto
from time import sleep
import win32gui


class uiautoXpress():
    """uiautomation控制码流卡
    """
    def GetWindow(self):
        # 获取窗口句柄
        hwnd = win32gui.GetForegroundWindow()
        # 获取窗口标题
        currentWindow = win32gui.GetWindowText(hwnd)
        return currentWindow

    def SetStream(self, stream):
        super().__init__()
        path = os.path.join(os.getcwd(), "stream")
        print(path)
        auto.uiautomation.DEBUG_SEARCH_TIME = True
        # 设置全局搜索超时时间
        auto.uiautomation.SetGlobalSearchTimeout(2)
        # 从任务栏获取码流卡窗口
        task = auto.PaneControl(searchDepth=1, Name="任务栏").PaneControl(Name="DesktopWindowXamlSource").\
            PaneControl(Class="Windows.UI.Input.InputSite.WindowClass")\
            .PaneControl(AutomationId="TaskbarFrame").GroupControl(AutomationId="TaskbarFrameRepeater")

        task.ButtonControl(searchDepth=1, AutomationId="{7C5A40EF-A0FB-4BFC-874A-C0F2E0B9FA8E}\DekTec\StreamXpress\StreamXpress.exe").Click(waitTime=0.01)
        sleep(3)
        currentWindow = self.GetWindow()
        # 判断任务栏点击StreamXpress后是否显示在前台
        if "StreamXpress" not in currentWindow:

            task.ButtonControl(searchDepth=1,
                               AutomationId="{7C5A40EF-A0FB-4BFC-874A-C0F2E0B9FA8E}\DekTec\StreamXpress\StreamXpress.exe").Click(
                waitTime=0.01)
            sleep(3)
            currentWindow = self.GetWindow()
        print("Current window:"+currentWindow)
        XpressWindow = auto.WindowControl(searchDepth=1, Name=currentWindow, desc='DekTec StreamXpress - Stream Player对话框')
        # 激活窗口
        XpressWindow.SetActive()
        # 设置为顶层
        XpressWindow.SetTopmost(True)
        # 设置码流制式
        XpressWindow.ComboBoxControl(AutomationId="2372", desc="Modulation").Click(waitTime=0.01)
        sleep(1)
        XpressWindow.ListItemControl(Name=" DVB-C").Click(waitTime=0.01)
        sleep(1)
        # 设置码流
        XpressWindow.ButtonControl(AutomationId='2328', Name="Open").Click(waitTime=0.02)
        sleep(1)
        # 双击选择码流
        XpressWindow.ListItemControl(Name=stream).DoubleClick(waitTime=0.01)

        XpressWindow.ButtonControl(AutomationId="2407", desc="Stop").Click(waitTime=0.01)
        sleep(1)
        XpressWindow.ButtonControl(AutomationId="2406", desc="Start").Click(waitTime=0.01)
        # 截图
        sleep(1)
        XpressWindow.CaptureToImage('Xpress.png')
		# 码流设置结束后码流卡最小化
        task.ButtonControl(searchDepth=1, AutomationId="{7C5A40EF-A0FB-4BFC-874A-C0F2E0B9FA8E}\DekTec\StreamXpress\StreamXpress.exe").Click(waitTime=0.01)


if __name__ == '__main__':
    ui = uiautoXpress()
    stream = argv[1]
    ui.SetStream(stream)

inspect的具体使用可参考: https://learn.microsoft.com/zh-cn/windows/win32/winauto/inspect-objects

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值