This module is for UIAutomatoin on Windows(Windows XP with SP3, Windows Vista, Windows 7 and Windows 8/8.1/10). It supports UIAutomatoin for the applications which implmented UIAutomation Provider, such as MFC, Windows Form, WPF, Modern UI(Metro UI), Qt and Firefox.
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.
Only uiautomation.py and the dll files are needed for UIAutomation. Other scripts are all demos. You can install uiautomation by "pip install uiautomation"
Run 'uiautomation.py -h' for help. Run automate_notepad_py3.py to see a simple demo.
If "RuntimeError: Can not get an instance of IUIAutomation" occured when running uiautomation.py, You need to install update KB971513 for your Windows. You can also download it from here https://github.com/yinkaisheng/WindowsUpdateKB971513ForIUIAutomation
By the way, You'd better run python as administrator. Otherwise uiautomation may fail to enumerate controls under some circumstances.
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]
How to use uiautomation?
run 'automation.py -h' or 'uiautomation.py -h'
Understand the arguments of uiautomation, and try the following examples
uiautomation.py -r -d 1 -t 0, print desktop(the root of control tree) and it's children(top level windows)
uiautomation.py -t 0, print current active window's controls
run notepad.exe, run uiautomation.py -t 3, swith to Notepad and wait for 5 seconds
uiautomation will print the controls of Notepad and save them to @AutomationLog.txt:
ControlType: WindowControl ClassName: Notepad
ControlType: EditControl ClassName: Edit
ControlType: ScrollBarControl ClassName:
ControlType: ButtonControl ClassName:
ControlType: ButtonControl ClassName:
ControlType: ScrollBarControl ClassName:
ControlType: ButtonControl ClassName:
ControlType: ButtonControl ClassName:
ControlType: ThumbControl ClassName:
ControlType: StatusBarControl ClassName:
ControlType: TextControl ClassName:
ControlType: TextControl ClassName:
...
run the following code
import subprocess
import uiautomation as automation
print(automation.GetRootControl())
subprocess.Popen('notepad.exe')
notepadWindow = automation.WindowControl(searchDepth = 1, ClassName = 'Notepad')
print(notepadWindow.Name)
notepadWindow.SetTopmost(True)
edit = notepadWindow.EditControl()
edit.SetValue('Hello')
edit.SendKeys('{Ctrl}{End}{Enter}World')
automation.GetRootControl() returns the root control
automation.WindowControl(searchDepth = 1, ClassName = 'Notepad') creates a WindowControl, the parameters specify how to search the control
the following parameters can be used
searchFromControl = None,
searchDepth = 0xFFFFFFFF,
searchWaitTime = SEARCH_INTERVAL,
foundIndex = 1
Name
ClassName
AutomationId
ControlType
Depth
See Control.__init__ for the comment of the parameters
See automation_notepad_py3.py for a detailed example
Another UI tool inspectX86.exe or inspectX64.exe supplied by Microsoft can also be used to see the UI elements.
Inspect (Inspect.exe) is a Windows-based tool that enables you select any UI element and view the element's accessibility data. You can view Microsoft UI Automation properties and control patterns, as well as Microsoft Active Accessibility properties. Inspect also enables you to test the navigational structure of the automation elements in the UI Automation tree, and the accessible objects in the Microsoft Active Accessibility hierarchy.
Inspect is installed with the Windows Software Development Kit (SDK) for Windows 8. (It is also available in previous versions of Windows SDK.) It is located in the \bin<platform> folder of the SDK installation path (Inspect.exe).
Some screenshots: