# The uiautomation module
:cn:[中文版介绍](https://github.com/yinkaisheng/Python-UIAutomation-for-Windows/blob/master/readme_cn.md)
Do not use 3.7.6 and 3.8.1, comtypes doesn't work in these two versions. Install an earlier version or the latest version.
https://github.com/enthought/comtypes/issues/202
This module is for [UIAutomation](https://docs.microsoft.com/en-us/windows/win32/winauto/ui-automation-specification) on Windows(Windows XP with SP3, Windows Vista, Windows 7 and Windows 8/8.1/10).
It supports UIAutomation for the applications which implmented UIAutomation Provider, such as MFC, Windows Form, WPF, Modern UI(Metro UI), Qt(Partly), Firefox(**version<=56 or >=60**), Chrome and Electron based apps(require **--force-renderer-accessibility** command line parameter).
I developed it in my spare time and for my personal use.
uiautomation is shared under the Apache Licence 2.0.
This means that the code can be freely copied and distributed, and costs nothing to use.
uiautomation1.x supports py2, py3 and doesn't depend on any third package.
uiautomation2.0+ only supports py3 and depends on comtypes and typing(Python3.5+ built-in).
uiautomation2.0+ is not backward compatible with early versions. See [API changes](https://github.com/yinkaisheng/Python-UIAutomation-for-Windows/blob/master/API%20changes.txt).
You can install uiautomation by "pip install uiautomation". After installation, a automation.py that calls uiautomation will be in 'C:\PythonXX\Scripts\'.
You use this script to traverse UI controls.
Run 'C:\PythonXX\Scripts\automation.py -h' for help.
Run demos\automation_calculator.py to see a simple demo.
On Windows 8/8.1, to automate a Metro App, the app must be in foreground. If a Metro App was switched to background, uiautomation can't fetch its controls' information.
By the way, You should run python as **administrator**. Otherwise uiautomation may fail to enumerate controls or get controls' information on Windows 7 or higher.
[Requirements:](https://docs.microsoft.com/en-us/windows/win32/api/uiautomationclient/nn-uiautomationclient-iuiautomation)
Microsoft UIAutomation Minimum supported client:
Windows 7, Windows Vista with SP2 and Platform Update for Windows Vista, Windows XP with SP3 and Platform Update for Windows Vista [desktop apps only]
Microsoft UIAutomation Minimum supported server:
Windows Server 2008 R2, Windows Server 2008 with SP2 and Platform Update for Windows Server 2008, Windows Server 2003 with SP2 and Platform Update for Windows Server 2008 [desktop apps only]
C++ dll source code: [UIAutomationClient](https://github.com/yinkaisheng/UIAutomationClient)
---
How to use uiautomation?
run '**automation.py -h**'
![help](images/uiautomation-h.png)
Understand the arguments of automation.py, and try the following examples:
**automation.py -t 0 -n**, print current active window's controls, show fullname
**automation.py -r -d 1 -t 0**, print desktop(the root of control tree) and it's children(top level windows)
![top level windows](images/automation_toplevels.png)
automation.py prints the properties of controls and the patterns they support.
You use controls and patterns to get controls' information and operate them.
A control should support some patterns or conditionally supports some patterns according to its control type.
![patterns](images/control_pattern.png)
Refer [Control Pattern Mapping for UI Automation Clients](https://docs.microsoft.com/en-us/windows/win32/winauto/uiauto-controlpatternmapping) for the full control pattern table.
uiautomation searches controls from the control tree based on the controls' properties you supply.
Suppose the control tree is
root(Name='Desktop', Depth=0)
window1(Depth=1)
control1-001(Depth=2)
control1-...(Depth=2)
...
control1-100(Depth=2)
window2(Name='window2', Depth=1)
control2-1(Depth=2)
control2-1-001(Depth=3)
control2-1-...(Depth=3)
...
control2-1-100(Depth=3)
control2-2(Depth=2)
control2-3(Depth=2)
control2-4(Name='2-4', Depth=2)
editcontrol(Name='myedit1', Depth=3)
**editcontrol(Name='myedit2', Depth=3)**
If you want to find the EditControl whose name is 'myedit2' and type 'hi',
you can write the following code:
```python
uiautomation.EditControl(searchDepth=3, Name='myedit2').SendKeys('hi')
```
But this code run slowly because there are more than 200 controls before myedit2 in the control tree.
uiautomation has to traverse more than 200 controls before finding myedit2 if search from root in 3 search depth.
The better is:
```python
window2 = uiautomation.WindowControl(searchDepth=1, Name='window2') # search 2 times
sub = window2.Control(searchDepth=1, Name='2-4') # search 4 times
edit = sub.EditControl(searchDepth=1, Name='myedit2') # search 2 times
edit.SendKeys('hi')
```
This code run faster than the former.
You can also combine the four lines code into one line.
```python
uiautomation.WindowControl(searchDepth=1, Name='window2').Control(searchDepth=1, Name='2-4').EditControl(searchDepth=1, Name='myedit2').SendKeys('hi')
```
Now let's take notepad.exe for an example.
Luanch notepad.exe and run automation.py -t 3, then swith to Notepad and wait for 5 seconds
automation.py will print the controls of Notepad and save them to @AutomationLog.txt:
ControlType: PaneControl ClassName: #32769 Name: 桌面 Depth: 0 **(Desktop window, the root control)**
ControlType: WindowControl ClassName: Notepad Depth: 1 **(Top level window)**
ControlType: EditControl ClassName: Edit Depth: 2
ControlType: ScrollBarControl ClassName: Depth: 3
ControlType: ButtonControl ClassName: Depth: 4
ControlType: ButtonControl ClassName: Depth: 4
ControlType: ThumbControl ClassName: Depth: 3
ControlType: TitleBarControl ClassName: Depth: 2
ControlType: MenuBarControl ClassName: Depth: 3
ControlType: MenuItemControl ClassName: Depth: 4
ControlType: ButtonControl ClassName: Name: 最小化 Depth: 3 **(Minimize Button)**
ControlType: ButtonControl ClassName: Name: 最大化 Depth: 3 **(Maximize Button)**
ControlType: ButtonControl ClassName: Name: 关闭 Depth: 3 **(Close Button)**
...
Run the following code
```python
# -*- coding: utf-8 -*-
# this script only works with Win32 notepad.exe
# if you notepad.exe is the Windows Store version in Windows 11, you need to uninstall it.
import subprocess
import uiautomation as auto
def test():
print(auto.GetRootControl())
subprocess.Popen('notepad.exe', shell=True)
# you should find the top level window first, then find children from the top level window
notepadWindow = auto.WindowControl(searchDepth=1, ClassName='Notepad')
if not notepadWindow.Exists(3, 1):
print('Can not find Notepad window')
exit(0)
print(notepadWindow)
notepadWindow.SetTopmost(True)
# find the first EditControl in notepadWindow
edit = notepadWindow.EditControl()
# usually you don't need to catch exceptions
# but if you meet a COMError exception, put it in a try block
try:
# use value pattern to get or set value
edit.GetValuePattern().SetValue('Hello')# or edit.GetPattern(auto.PatternId.ValuePattern)
except auto.comtypes.COMError as ex:
# maybe you don't run python as administrator
# or the control doesn't have a implementation for the pattern method(I have no solution for this)
pass
edit.SendKeys('{Ctrl}{End}{Enter}World')
没有合适的资源?快使用搜索试试~ 我知道了~
uiautomation模块源码
共53个文件
py:25个
png:8个
txt:4个
1 下载量 114 浏览量
2023-09-26
22:33:42
上传
评论
收藏 2.06MB ZIP 举报
温馨提示
uiautomation模块源码
资源推荐
资源详情
资源评论
收起资源包目录
Python-UIAutomation-for-Windows-master.zip (53个子文件)
Python-UIAutomation-for-Windows-master
readme_cn.md 14KB
inspect
InspectX64.exe 290KB
Inspect is a tool in Windows SDK.txt 83B
InspectX86.exe 245KB
setup.py 1KB
.gitattributes 411B
uiautomation
__init__.py 97B
version.py 19B
bin
UIAutomationClient_VC140_X86.dll 83KB
UIAutomationClient_VC140_X64.dll 100KB
uiautomation.py 378KB
LICENSE 11KB
readme.md 14KB
rename_wheel.py 283B
document.html 2.4MB
requirements-dev.txt 14B
pydoc.bat 63B
MANIFEST.in 91B
.gitignore 86B
upload_pypi.bat 96B
setup.cfg 29B
images
uiautomation-h.png 14KB
automation_toplevels.png 39KB
automation_qq.png 35KB
word.png 18KB
rename_pdf_bookmark.gif 482KB
github_desktop.png 38KB
pretty_print_dir.png 19KB
yks-zfb.png 47KB
control_pattern.png 12KB
wireshark3.0.gif 771KB
demos
clipboard_test.py 2KB
automation_devicemanager.py 4KB
get_qq_group_members.py 3KB
automation_calculator.py 6KB
_scripts can be run without installing uiautomation.txt 26B
hide_window_by_hotkey.py 2KB
capture_screen.py 2KB
uiautomation_in_thread.py 2KB
pretty_print_dir.py 2KB
expand_tree_item_under_cursor.py 4KB
automation_wireshark_qt5.py 2KB
collapse_tree_item_under_cursor.py 2KB
hotkey_test.py 2KB
automation_help_demo.py 8KB
notepad_topmost.py 1KB
walk_tree_test.py 3KB
capture_image_by_hotkey.py 886B
automation_notepad.py 8KB
bitmap_test.py 4KB
automation.py 5KB
API changes.txt 5KB
scripts
automation.py 5KB
共 53 条
- 1
资源评论
1夜的终章1
- 粉丝: 90
- 资源: 14
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 上市公司-股票性质数据-工具变量(民企、国企、央企)2003-2022年.dta
- 上市公司-股票性质数据-工具变量(民企、国企、央企)2003-2022年.xlsx
- Reeds+Shepp曲线算法讲解和实现.pdf
- 毕业设计基于SpringBoot+MyBatisPlus+MySQL+Vue的外卖配送信息系统源代码+数据库
- 词向量(Word Embeddings)是自然语言处理(NLP)领域的一种重要技术.txt
- Surfer,线性函数
- MyBatis 的动态 SQL 是其核心特性之一.txt
- 时代的sdddsddsddsd
- 基于哈希链表的简单人员信息管理系统
- 其他类别JdonFramework开源框架 v5.1 Build20071025-jdonframework-5.1.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功