UI Automation in Python

Try using UI Automation to automate Windows GUI operations in Python. There are .NET version and COM version of UI Automation, but I want to use it from CPython, so use the COM version. For the time being, I checked how to call it, so make a note.

What is UI Automation?

UI Automation Recommendations http://blogs.msdn.com/b/japan_platform_sdkwindows_sdk_support_team_blog/archive/2011/05/26/ui-automation.aspx

UI Automation http://msdn.microsoft.com/en-us/library/ee684009.aspx

Preparation

First, put comtypes in pip,

pip install comtypes

Next, generate a wrapper for UI Automation.

import comtypes
from comtypes.client import GetModule
GetModule('UIAutomationCore.dll')

A wrapper module is generated in Lib \ site-packages \ comtypes \ gen. After that, compare the MSDN reference of UI Automation with _944DE083_8FB8_45CF_BCB7_C477ACB2F897_0_1_0.py to find out how to use it. (The file name may be different depending on the environment)

Get UI Automation objects

Import comtypes.gen.UIAutomationClient to get UI Automation objects and do this and that.

from comtypes.gen.UIAutomationClient import *
uia = CoCreateInstance(CUIAutomation._reg_clsid_,interface=IUIAutomation,clsctx=CLSCTX_INPROC_SERVER)

re = uia.GetRootElement()  #Get the top-level element of the desktop
print re.CurrentName       # 'desktop'

Calculator example

Try accessing the calculator (calc.exe) via UI Automation. Continuing from above.

import subprocess
calc = subprocess.Popen('calc.exe')    #Calculator start
#Identify the calculator from the child elements of the desktop by specifying the pid
cond = uia.CreatePropertyCondition(UIA_ProcessIdPropertyId, calc.pid)
calc_win = re.FindFirst(TreeScope_Children, cond)
print calc_win.CurrentName    # 'calculator'

For the time being, this time it is up to here. I haven't reached the automation of GUI operation, so maybe I'll continue next time.

Recommended Posts

UI Automation in Python
UI Automation Part 2 in Python
Quadtree in Python --2
Python in optimization
CURL in python
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Sorted list in Python
Daily AtCoder # 36 in Python
Clustering text in Python
Daily AtCoder # 2 in Python
Daily AtCoder # 32 in Python
Daily AtCoder # 6 in Python
Daily AtCoder # 18 in Python
Edit fonts in Python
Singleton pattern in Python
Read DXF in python
Daily AtCoder # 53 in Python
Use config.ini in Python
Daily AtCoder # 33 in Python
Solve ABC168D in Python
Logistic distribution in Python
Daily AtCoder # 7 in Python
LU decomposition in Python
One liner in Python