微软提供了关于自动化操作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
安装automation
1、安装uiautomation库
# pip install uiautomation
python中默认的安装目录为: C:/Users/XXX/AppData/Local/Programs/Python/Python310/Scripts
2、使用
https://blog.csdn.net/freeking101/article/details/105758387
automation.py -h 查看帮忙
automation.py -t 0, 打印当前激活窗口的所有控件
automation.py -r -d 1 -t 0, 打印桌面(树的根控件 )和它的第一层子窗口(TopLevel顶层窗口)
进入windwos PowerShell,到automation.py的目录;
通过命令: python automation.py +参数:
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
2)、抓取微信控件
-a,表述抓取所有
-t3,表示延迟3s抓取;
此处执行命令后,将鼠标移至微信,抓取信息如下:
PS C:/Users/gXX/AppData/Local/Programs/Python/Python310/Scripts> python automation.py -a -t3 UIAutomation 2.0.16 (Python 3.10.5, 64 bit) please wait for 3 seconds 2022-07-11 08:21:51.523 automation.py[79] main -> Starts, Current Cursor Position: (195, 354) 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: (12,151,1150,951)[1138x800] Name: 'WeChat' Handle: 0x90568(591208) Depth: 1 SupportedPattern: LegacyIAccessiblePattern ScrollItemPattern TransformPattern WindowPattern 2022-07-11 08:21:51.951 automation.py[114] main -> Ends PS C:/Users/goXXX/AppData/Local/Programs/Python/Python310/Scripts>
# 定义程序窗口
iautomation.WindowControl(Name=’WeChat’)
# 切到到程序窗口
iautomation.WindowControl(Name=’WeChat’).SwitchToThisWindow()
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/273565.html