微软提供了关于自动化操作PC端桌面应用程序的工具,官方描述为:

  Microsoft UI Automation is an accessibility framework that enables Windows applications to provide and consume programmatic information about user interfaces (UIs). It provides programmatic access to most UI elements on the desktop. It enables assistive technology products, such as screen readers, to provide information about the UI to end users and to manipulate the UI by means other than standard input. UI Automation also allows automated test scripts to interact with the UI.

  Python库为我们封装了shoppingmode 微软UI Automation的API,即uiautomation库。

资料

微软官网关于UI Automation的介绍:

1、IUIAutomationElement interface:

https://docs.microsoft.com/en-us/windows/win32/api/uiautomationclient/nn-uiautomationclient-iuiautomationelement

2、Control Types and Their Supported Control Patterns,查看全部的Control-Pattern支持表格。

https://docs.microsoft.com/en-us/windows/win32/winauto/uiauto-controlpatternmapping

3、uiautomation库的具体用法,GitHub上开发者发布的教程:

https://github.com/yinkaisheng/Python-UIAutomation-for-Windows/blob/master/readme_cn.md

4、作者自己也有博客

https://www.cnblogs.com/Yinkaisheng/p/3444132.html

安装automation

1、安装uiautomation库

# pip install uiautomation

python中默认的安装目录为: C:\Users\XXX\AppData\Local\Programs\Python\Python310\Scripts

2、使用

进入windwos PowerShell,到automation.py的目录,通过命令: python automation.py +参数:

因为加入和环境变量,也可以直接的cmd中,输入: automation.py -t3

1)、查看帮助

PS C:\Users\gXXX\AppData\Local\Programs\Python\Python310\Scripts> python automation.py -h
UIAutomation 2.0.16 (Python 3.10.5, 64 bit)
usage
-h show command help
-t delay time, default 3 seconds, begin to enumerate after Value seconds, this must be an integer
you can delay a few seconds and make a window active so automation can enumerate the active window
-d enumerate tree depth, this must be an integer, if it is null, enumerate the whole tree
-r enumerate from root:Desktop window, if it is null, enumerate from foreground window
-f enumerate from focused control, if it is null, enumerate from foreground window
-c enumerate the control under cursor, if depth is < 0, enumerate from its ancestor up to depth
-a show ancestors of the control under cursor
-n show control full name, if it is null, show first 30 characters of control's name in console,
always show full name in log file @AutomationLog.txt
-p show process id of controls

运行automation.py遍历控件时,支持下列参数

-t int value, 延迟时间time秒,延迟指定秒数再遍历控件,
-r, 从树的根部(root: Desktop)遍历,如果不指定,从当前窗口遍历
-d, int Value, 遍历控件树的的深度depth,如果不指定,遍历整个树,和-c一起使用时,可以为负值
-f, 遍历焦点focus控件,如果不指定,从当前窗口遍历
-c, 遍历光标下的控件,如果不指定,从当前窗口遍历,如果同时指定-d, -d可以为负值,比如-d-2表示从光标下控件向上找到两层父控件,遍历这个父控件

-a, 获取光标下控件及其所有祖先(ancestor)控件

-n, 显示控件的完整name, 如果不指定,只显示前30个字符

举例:

automation.py –t3, 3秒后遍历当前窗口所有控件
automation.py –d2 –t3, 3秒后遍历当前窗口前三层控件
automation.py –r –d1 –t0 -n, 0秒后遍历root的第一层子控件,并显示控件完整名称

automation.py –c –t3, 3秒后遍历鼠标光标下面的控件信息

automation.py –c –t3 -d-2, 3秒后遍历鼠标光标下面的控件向上两层的父控件

2)、抓取微信控件

-t3,表示延迟3s抓取;

此处执行命令后,将鼠标移至微信,抓取信息如下:


C:\Users\gongl1>automation.py -d4 -t3
UIAutomation 2.0.16 (Python 3.10.5, 64 bit)
please wait for 3 seconds

2022-07-11 11:58:54.240 automation.py[79] main -> Starts, Current Cursor Position: (285, 379)
ControlType: PaneControl ClassName: #32769 AutomationId: Rect: (0,0,1920,1080)[1920x1080] Name: 'Desktop 1' Handle: 0x10010(65552) Depth: 0 SupportedPattern: LegacyIAccessiblePattern
ControlType: WindowControl ClassName: WeChatMainWndForPC AutomationId: Rect: (30,95,1168,895)[1138x800] Name: 'WeChat' Handle: 0x20742(132930) Depth: 1 SupportedPattern: LegacyIAccessiblePattern ScrollItemPattern TransformPattern WindowPattern
ControlType: ButtonControl ClassName: AutomationId: Rect: (41,140,86,185)[45x45] Name: '磊' Handle: 0x0(0) Depth: 5 SupportedPattern: InvokePattern LegacyIAccessiblePattern ScrollItemPattern
ControlType: ButtonControl ClassName: AutomationId: Rect: (32,204,95,259)[63x55] Name: 'Chats' Handle: 0x0(0) Depth: 5 SupportedPattern: InvokePattern LegacyIAccessiblePattern ScrollItemPattern
ControlType: ButtonControl ClassName: AutomationId: Rect: (32,255,95,310)[63x55] Name: 'Contacts' Handle: 0x0(0) Depth: 5 SupportedPattern: InvokePattern LegacyIAccessiblePattern ScrollItemPattern
ControlType: ButtonControl ClassName: AutomationId: Rect: (32,306,95,361)[63x55] Name: 'Favorites' Handle: 0x0(0) Depth: 5 SupportedPattern: InvokePattern LegacyIAccessiblePattern ScrollItemPattern
ControlType: ButtonControl ClassName: AutomationId: Rect: (32,357,95,412)[63x55] Name: 'WeChat Files' Handle: 0x0(0) Depth: 5 SupportedPattern: InvokePattern LegacyIAccessiblePattern ScrollItemPattern
ControlType: ButtonControl ClassName: AutomationId: Rect: (32,408,95,463)[63x55] Name: 'Moments' Handle: 0x0(0) Depth: 5 SupportedPattern: InvokePattern LegacyIAccessiblePattern ScrollItemPattern
ControlType: ButtonControl ClassName: AutomationId: Rect: (32,561,95,616)[63x55] Name: 'Search' Handle: 0x0(0) Depth: 5 SupportedPattern: InvokePattern LegacyIAccessiblePattern ScrollItemPattern
ControlType: ButtonControl ClassName: AutomationId: Rect: (48,741,79,772)[31x31] Name: 'Mini Programs Panel' Handle: 0x0(0) Depth: 5 SupportedPattern: InvokePattern LegacyIAccessiblePattern ScrollItemPattern
2022-07-11 11:58:55.270 automation.py[114] main -> Ends

C:\Users\gongl1>

 

这些参数就对应:

对于未读消息,可以查看到:

ScrollItemPattern
ControlType: ListControl ClassName: AutomationId: Rect: (82,212,394,934)[312x722] Name: '会话' Handle: 0x0(0) Depth: 9 ScrollPattern.HorizontalScrollPercent: 0.0 ScrollPattern.VerticalScrollPercent: 0.0060790273556231 SupportedPattern: LegacyIAccessiblePattern ScrollItemPattern ScrollPattern SelectionPattern
ControlType: ListItemControl ClassName: AutomationId: Rect: (82,212,394,292)[312x80] Name: '群test' Handle: 0x0(0) Depth: 10 SelectionItemPattern.IsSelected: False ValuePattern.Value: '张三:未读信息' SupportedPattern: LegacyIAccessiblePattern ScrollItemPattern

# 绑定微信的主程序窗口

wx=iautomation.WindowControl(Name='WeChat')

# 切到到程序窗口

wx.SwitchToThisWindow()

# 绑定会话窗口

hw=wx.ListControl(Name='Chats')

python~windows自动化工具 uiautomation库的更多相关文章

  1. Python 任务自动化工具:nox 的配置与 API

    英文 | Configuration & API 出处 | nox 官方文档 译者 | 豌豆花下猫@Python猫 Github地址:https://github.com/chinesehua ...

  2. Python 任务自动化工具 tox 教程

    在我刚翻译完的 Python 打包系列文章中,作者提到了一个神奇的测试工具 tox,而且他本人就是 tox 的维护者之一.趁着话题的相关性,本文将对它做简单的介绍,说不定大家在开发项目时能够用得上. ...

  3. 强大的 Python 任务自动化工具!invoke 十分钟入门指南

    接着前面的<tox 教程>,以及刚翻译好的<nox文档>,我们继续聊聊 Python 任务自动化的话题. nox 的作者在去年的 Pycon US 上,做了一场题为<Br ...

  4. Python第三方常用工具、库、框架等

    Python ImagingLibrary(PIL):它提供强大的图形处理的能力,并提供广泛的图形文件格式支持,该库能进行图形格式的转换.打印和显示.还能进行一些图形效果的处理,如图形的放大.缩小和旋 ...

  5. python windows时间同步工具

    由于某种原因(BIOS电池没电),电脑的系统时间会与我们的北京时间不同步,将会导致如下问题: 1. 抢火车票的时候已经过时间了 2.别的同事都走了,你还以为没下班 …… 规避问题的方法:同步系统时间 ...

  6. python接口自动化测试之requests库详解

    前言 说到python发送HTTP请求进行接口自动化测试,脑子里第一个闪过的可能就是requests库了,当然python有很多模块可以发送HTTP请求,包括原生的模块http.client,urll ...

  7. 更好用的 Python 任务自动化工具:nox 官方教程

    英文| nox tutorial 出处| nox 官方文档 译者| 豌豆花下猫@Python猫 Github地址:https://github.com/chinesehuazhou/nox_doc_c ...

  8. 开源自己用python封装的一个Windows GUI(UI Automation)自动化工具,支持MFC,Windows Forms,WPF,Metro,Qt

    首先,大家可以看下这个链接 Windows GUI自动化测试技术的比较和展望 . 这篇文章介绍了Windows中GUI自动化的三种技术:Windows API, MSAA - Microsoft Ac ...

  9. Python测试 ——开发工具库

    Web UI测试自动化 splinter - web UI测试工具,基于selnium封装. selenium - web UI自动化测试. mechanize- Python中有状态的程序化Web浏 ...

  10. Python渗透测试工具库

    漏洞及渗透练习平台 WebGoat漏洞练习平台: https://github.com/WebGoat/WebGoat webgoat-legacy漏洞练习平台: https://github.com ...

随机推荐

  1. 【.NET源码解读】深入剖析中间件的设计与实现

    .NET本身就是一个基于中间件(middleware)的框架,它通过一系列的中间件组件来处理HTTP请求和响应.在之前的文章<.NET源码解读kestrel服务器及创建HttpContext对象 ...

  2. PHP如何获取短网址跳转之前的网址?

    现在有很多的短网址应用,点击就跳转到缩短之前的网址了,那今天我们就来看一下,PHP如何**短网址跳转之前的网址,其实也很简单,就是用php的get_headers 函数,取得Response Head ...

  3. Kotlin 常用语法糖记录

    原文地址: Kotlin 常用语法糖记录 - Stars-One的杂货小窝 当使用 Kotlin 编程时,有一些常用的函数可以帮助我们简化代码并提高开发效率. 稍微列举下常用的方法 runCatchi ...

  4. CF103B Cthulhu题解

    CF103B Cthulhu 点击查看题目 点击查看思路 如果 \(n < m\),那么会形成多个环. 如果 \(n > m\),那么不会形成环. 只有 \(n = m\) 时会形成环, ...

  5. 【线上技术分享】即构&MobTech袤博移动游戏开发者全能进阶沙龙

    游戏行业的兴起与当前移动互联网用户碎片化.休闲化的生活特征密不可分,在用户旺盛的需求下,游戏行业迎来了绝佳的发展机遇,今年上半年已多款游戏DAU过亿. 市场的火爆也为游戏行业带来了异常激烈的竞争,加上 ...

  6. Lock同步_小记

    使用同步机制的这种方式解决线程安全问题,但是不知道具体的锁对象在哪里添加,并且锁对象在哪里释放锁对象,对于这种情况Jdk5以后Java提供了一个更具体的锁对象:Lock Lock 实现提供了比使用 s ...

  7. Redis和Mysql保持数据一致性

    1.简述   在高并发的场景下,大量的请求直接访问Mysql很容易造成性能问题.所以,我们都会用Redis来做数据的缓存,削减对数据库的请求.但是,Mysql和Redis是两种不同的数据库,如何保证不 ...

  8. 好用工具: windows terminal

    直接在微软商店搜索该软件即可,本文介绍无法使用微软商店的情况. 解题思路 当我们无法下载某个软件时,可直接去Github中寻找该项目,知道该软件资源并下载. 下载地址 https://github.c ...

  9. html标签tr td是什么意思

    <table>代表表格</table><tr>代表表格中的一行</tr><td>代表表格中的一列</td>'tr'与'td'交成 ...

  10. CSSRelated

    CSS 几种常用的清除浮动方法 ️️️ 父级 div 定义伪类:after 和 zoom; /* 这个class名指的是需要清除浮动的父级 */ .clearfloat:after { display ...