棒棒的学习博客:
http://git.oschina.net/yinkaisheng/PythonUIAutomation4Windows
http://blog.csdn.net/sogouauto/article/details/43303403
http://www.cnblogs.com/kangyi/tag/UI%20Automation/
http://www.cnblogs.com/coderzh/archive/2009/11/14/1603109.html
http://blog.csdn.net/ffeiffei/article/details/6637418
python调用UIA的步骤
Python调用UIA,需要使用到UIAutomationCore.dll,它所在的位置为C:\WINDOWS\system32\uiautomationcore.dll,此Dll在Windows XP以上的版本都存在,另外Python调用UIA使用到的Windows SDK位置:C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0\Include。
Python调用UIA的步骤:
1、加载UIAutomationCore.dll
UIAutomationClient = GetModule("UIAutomationCore.dll")
2、获取IUIAutomation对象
IUIAutomation = CreateObject("{ff48dba4-60ef-4201-aa87-54103eef594e}", interface=UIAutomationClient.IUIAutomation)
3、获取RootElement(桌面)
root = IUIAutomation.GetRootElement()
4、调用FindFirst,查找控件
elm = root.FindFirst(UIAutomationClient.TreeScope_Descendants, cnd2)
5、获取Pattern,调用Invoke,实现点击操作
ppt = elm.GetCurrentPattern(UIAutomationClient.UIA_InvokePatternId)
printppt
testtt = cast(ppt, POINTER(UIAutomationClient.IUIAutomationInvokePattern))
testtt.Invoke()
完整的代码
#coding=utf-8
fromctypesimport*
#dll = CDLL("UIAutomationCore.dll")
#print dll
fromcomtypesimportGUID
fromcomtypes.clientimportCreateObject, GetModule
UIAutomationClient = GetModule("UIAutomationCore.dll")
IUIAutomation = CreateObject("{ff48dba4-60ef-4201-aa87-54103eef594e}", interface=UIAutomationClient.IUIAutomation)
root = IUIAutomation.GetRootElement()
printroot
printroot.CurrentClassName
printroot.CurrentName
printroot.CurrentBoundingRectangle
printroot.CurrentProcessId
### this produce uninitialized pointer
#cnd1 = IUIAutomation.CreatePropertyConditionEx(UIAutomationClient.UIA_ControlTypePropertyId,
#'UIA_EditControlTypeId',UIAutomationClient.PropertyConditionFlags_None)
#print cnd1
cnd2 = IUIAutomation.CreatePropertyConditionEx(UIAutomationClient.UIA_NamePropertyId,
u"电影",UIAutomationClient.PropertyConditionFlags_None)
printcnd2
#cnd = IUIAutomation.CreateAndCondition(cnd1, cnd2)
elm = root.FindFirst(UIAutomationClient.TreeScope_Descendants, cnd2)
printelm.CurrentProcessId
printelm.CurrentName
#ppt1 = elm.GetCurrentPatternAs(UIAutomationClient.UIA_InvokePatternId, GUID("{fb377fbe-8ea6-46d5-9c73-6499642d3059}"))
#print hex(ppt1)
#p = POINTER(UIAutomationClient.IUIAutomationInvokePattern)
#p.from_address(ppt1)
#IUIAutomationInvokePattern
#IUnknown
#test = Test.from_address(ppt)
##ppt = elm.GetCurrentPattern(UIAutomationClient.UIA_InvokePatternId)
#print test.id
####强制转换类型
ppt = elm.GetCurrentPattern(UIAutomationClient.UIA_InvokePatternId)
printppt
testtt = cast(ppt, POINTER(UIAutomationClient.IUIAutomationInvokePattern))
testtt.Invoke()