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.
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
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)
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'
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