This repository has been archived by the owner on Nov 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
pyuiautomation.py
303 lines (245 loc) · 11.8 KB
/
pyuiautomation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
from __future__ import print_function, unicode_literals
import sys
from collections import deque
from xml.dom import minidom
import comtypes
import comtypes.client
__version__ = '1.0.1'
PY_3 = sys.version_info > (3,)
if not PY_3:
range = xrange
str = unicode
UIAutomationClient = comtypes.client.GetModule('UIAutomationCore.dll')
# noinspection PyProtectedMember
_IUIAutomation = comtypes.CoCreateInstance(
UIAutomationClient.CUIAutomation._reg_clsid_,
interface=UIAutomationClient.IUIAutomation,
clsctx=comtypes.CLSCTX_INPROC_SERVER
)
_control_type = {
UIAutomationClient.UIA_ButtonControlTypeId: 'UIA_ButtonControlTypeId',
UIAutomationClient.UIA_CalendarControlTypeId: 'UIA_CalendarControlTypeId',
UIAutomationClient.UIA_CheckBoxControlTypeId: 'UIA_CheckBoxControlTypeId',
UIAutomationClient.UIA_ComboBoxControlTypeId: 'UIA_ComboBoxControlTypeId',
UIAutomationClient.UIA_CustomControlTypeId: 'UIA_CustomControlTypeId',
UIAutomationClient.UIA_DataGridControlTypeId: 'UIA_DataGridControlTypeId',
UIAutomationClient.UIA_DataItemControlTypeId: 'UIA_DataItemControlTypeId',
UIAutomationClient.UIA_DocumentControlTypeId: 'UIA_DocumentControlTypeId',
UIAutomationClient.UIA_EditControlTypeId: 'UIA_EditControlTypeId',
UIAutomationClient.UIA_GroupControlTypeId: 'UIA_GroupControlTypeId',
UIAutomationClient.UIA_HeaderControlTypeId: 'UIA_HeaderControlTypeId',
UIAutomationClient.UIA_HeaderItemControlTypeId: 'UIA_HeaderItemControlTypeId',
UIAutomationClient.UIA_HyperlinkControlTypeId: 'UIA_HyperlinkControlTypeId',
UIAutomationClient.UIA_ImageControlTypeId: 'UIA_ImageControlTypeId',
UIAutomationClient.UIA_ListControlTypeId: 'UIA_ListControlTypeId',
UIAutomationClient.UIA_ListItemControlTypeId: 'UIA_ListItemControlTypeId',
UIAutomationClient.UIA_MenuBarControlTypeId: 'UIA_MenuBarControlTypeId',
UIAutomationClient.UIA_MenuControlTypeId: 'UIA_MenuControlTypeId',
UIAutomationClient.UIA_MenuItemControlTypeId: 'UIA_MenuItemControlTypeId',
UIAutomationClient.UIA_PaneControlTypeId: 'UIA_PaneControlTypeId',
UIAutomationClient.UIA_ProgressBarControlTypeId: 'UIA_ProgressBarControlTypeId',
UIAutomationClient.UIA_RadioButtonControlTypeId: 'UIA_RadioButtonControlTypeId',
UIAutomationClient.UIA_ScrollBarControlTypeId: 'UIA_ScrollBarControlTypeId',
UIAutomationClient.UIA_SeparatorControlTypeId: 'UIA_SeparatorControlTypeId',
UIAutomationClient.UIA_SliderControlTypeId: 'UIA_SliderControlTypeId',
UIAutomationClient.UIA_SpinnerControlTypeId: 'UIA_SpinnerControlTypeId',
UIAutomationClient.UIA_SplitButtonControlTypeId: 'UIA_SplitButtonControlTypeId',
UIAutomationClient.UIA_StatusBarControlTypeId: 'UIA_StatusBarControlTypeId',
UIAutomationClient.UIA_TabControlTypeId: 'UIA_TabControlTypeId',
UIAutomationClient.UIA_TabItemControlTypeId: 'UIA_TabItemControlTypeId',
UIAutomationClient.UIA_TableControlTypeId: 'UIA_TableControlTypeId',
UIAutomationClient.UIA_TextControlTypeId: 'UIA_TextControlTypeId',
UIAutomationClient.UIA_ThumbControlTypeId: 'UIA_ThumbControlTypeId',
UIAutomationClient.UIA_TitleBarControlTypeId: 'UIA_TitleBarControlTypeId',
UIAutomationClient.UIA_ToolBarControlTypeId: 'UIA_ToolBarControlTypeId',
UIAutomationClient.UIA_ToolTipControlTypeId: 'UIA_ToolTipControlTypeId',
UIAutomationClient.UIA_TreeControlTypeId: 'UIA_TreeControlTypeId',
UIAutomationClient.UIA_TreeItemControlTypeId: 'UIA_TreeItemControlTypeId',
UIAutomationClient.UIA_WindowControlTypeId: 'UIA_WindowControlTypeId'
}
_tree_scope = {
'ancestors': UIAutomationClient.TreeScope_Ancestors,
'children': UIAutomationClient.TreeScope_Children,
'descendants': UIAutomationClient.TreeScope_Descendants,
'element': UIAutomationClient.TreeScope_Element,
'parent': UIAutomationClient.TreeScope_Parent,
'subtree': UIAutomationClient.TreeScope_Subtree
}
class _UIAutomationElement(object):
def __init__(self, IUIAutomationElement):
self.IUIAutomationElement = IUIAutomationElement
@property
def CurrentAutomationId(self):
"""Retrieves the UI Automation identifier of the element
:rtype : unicode
"""
return self.IUIAutomationElement.CurrentAutomationId
AutomationId = CurrentAutomationId
@property
def CurrentBoundingRectangle(self):
"""Retrieves the coordinates of the rectangle that completely encloses
the element.
Returns tuple (left, top, right, bottom)
:rtype : tuple
"""
rect = self.IUIAutomationElement.CurrentBoundingRectangle
return rect.left, rect.top, rect.right, rect.bottom
BoundingRectangle = CurrentBoundingRectangle
@property
def CurrentClassName(self):
"""Retrieves the class name of the element
:rtype : unicode
"""
return self.IUIAutomationElement.CurrentClassName
ClassName = CurrentClassName
@property
def CurrentControlType(self):
"""Retrieves the control type of the element
:rtype : int
"""
return self.IUIAutomationElement.CurrentControlType
ControlType = CurrentControlType
@property
def CurrentControlTypeName(self):
"""Retrieves the name of the control type of the element.
Ref: http://msdn.microsoft.com/en-us/library/windows/desktop/ee671198(v=vs.85).aspx
:rtype : str
"""
return _control_type[self.CurrentControlType]
ControlTypeName = CurrentControlTypeName
@property
def CurrentIsEnabled(self):
"""Indicates whether the element is enabled
:rtype : bool
"""
return bool(self.IUIAutomationElement.CurrentIsEnabled)
IsEnabled = CurrentIsEnabled
@property
def CurrentName(self):
"""Retrieves the name of the element
:rtype : unicode
"""
return self.IUIAutomationElement.CurrentName
Name = CurrentName
def GetClickablePoint(self):
"""Retrieves a point on the element that can be clicked
Returns tuple (x, y) if a clickable point was retrieved,
or None otherwise
:rtype : tuple
"""
point = self.IUIAutomationElement.GetClickablePoint()
if point[1]:
return point[0].x, point[0].y
else:
return None
def _build_condition(self, Name, ControlType, AutomationId):
condition = _IUIAutomation.CreateTrueCondition()
if Name is not None:
name_condition = _IUIAutomation.CreatePropertyCondition(
UIAutomationClient.UIA_NamePropertyId, Name
)
condition = _IUIAutomation.CreateAndCondition(
condition, name_condition
)
if ControlType is not None:
control_type_condition = _IUIAutomation.CreatePropertyCondition(
UIAutomationClient.UIA_ControlTypePropertyId, ControlType
)
condition = _IUIAutomation.CreateAndCondition(
condition, control_type_condition
)
if AutomationId is not None:
automation_id_condition = _IUIAutomation.CreatePropertyCondition(
UIAutomationClient.UIA_AutomationIdPropertyId, AutomationId)
condition = _IUIAutomation.CreateAndCondition(
condition, automation_id_condition
)
return condition
def findfirst(self, tree_scope, Name=None, ControlType=None,
AutomationId=None):
"""Retrieves the first child or descendant element that matches
specified conditions
Returns None if there is no element that matches specified conditions
If Name is None, element with any name will match
If ControlType is None, element with any control type will match
:param tree_scope: Should be one of 'element', 'children',
'descendants', 'parent', 'ancestors', 'subtree'.
Ref: http://msdn.microsoft.com/en-us/library/windows/desktop/ee671699(v=vs.85).aspx
:type tree_scope: str
:param Name: Name of the element.
:type Name: str
:param ControlType: Control type of the element
(one of UIAutomationClient.UIA_*ControlTypeId).
Ref: http://msdn.microsoft.com/en-us/library/windows/desktop/ee671198(v=vs.85).aspx
:type ControlType: int
:param AutomationId: UI Automation identifier (ID)
for the automation element.
Ref: http://msdn.microsoft.com/en-us/library/windows/desktop/ee695997(v=vs.85).aspx
:type AutomationId: str
:rtype : _UIAutomationElement
"""
tree_scope = _tree_scope[tree_scope]
condition = self._build_condition(Name, ControlType, AutomationId)
element = self.IUIAutomationElement.FindFirst(tree_scope, condition)
return _UIAutomationElement(element) if element else None
def findall(self, tree_scope, Name=None, ControlType=None,
AutomationId=None):
"""Returns list of UI Automation elements that satisfy specified
conditions
Returns empty list if there are no elements that matches specified
conditions.
If Name is None, elements with any name will match
If ControlType is None, elements with any control type will match
:param tree_scope: Should be one of 'element', 'children',
'descendants', 'parent', 'ancestors', 'subtree'.
Ref: http://msdn.microsoft.com/en-us/library/windows/desktop/ee671699(v=vs.85).aspx
:type tree_scope: str
:param Name: Name of the element.
:type Name: str
:param ControlType: Control type of the element
(one of UIAutomationClient.UIA_*ControlTypeId).
Ref: http://msdn.microsoft.com/en-us/library/windows/desktop/ee671198(v=vs.85).aspx
:type ControlType: int
:param AutomationId: UI Automation identifier (ID) for the automation
element.
Ref: http://msdn.microsoft.com/en-us/library/windows/desktop/ee695997(v=vs.85).aspx
:type AutomationId: str
:rtype : list
"""
tree_scope = _tree_scope[tree_scope]
condition = self._build_condition(Name, ControlType, AutomationId)
IUIAutomationElementArray = self.IUIAutomationElement.FindAll(
tree_scope, condition)
return [_UIAutomationElement(IUIAutomationElementArray.GetElement(i))
for i in range(IUIAutomationElementArray.Length)]
def Invoke(self):
IUnknown = self.IUIAutomationElement.GetCurrentPattern(
UIAutomationClient.UIA_InvokePatternId)
IUIAutomationInvokePattern = IUnknown.QueryInterface(
UIAutomationClient.IUIAutomationInvokePattern)
IUIAutomationInvokePattern.Invoke()
def __str__(self):
return '<%s (Name: %s, Class: %s, AutomationId: %s>' % (
self.CurrentControlTypeName, self.CurrentName,
self.CurrentClassName, self.CurrentAutomationId)
def toxml(self):
xml = minidom.Document()
queue = deque()
queue.append((self, xml))
while queue:
element, xml_node = queue.popleft()
xml_element = minidom.Element(element.CurrentControlTypeName)
xml_element.setAttribute('Name', str(element.CurrentName))
xml_element.setAttribute('AutomationId',
str(element.CurrentAutomationId))
xml_element.setAttribute('ClassName',
str(element.CurrentClassName))
xml_element.ownerDocument = xml
xml_node.appendChild(xml_element)
for child in element.findall('children'):
queue.append((child, xml_element))
return xml.toprettyxml()
def ElementFromHandle(handle):
return _UIAutomationElement(_IUIAutomation.ElementFromHandle(handle))
def GetRootElement():
return _UIAutomationElement(_IUIAutomation.GetRootElement())