Gridの取得を試みる
UI Automationに
GridPatternクラスや
GridItemPatternクラスが設けられている。
しかしPythonでこれらのクラスの使ってデータを取得する方法がわからない…
暫定的に対応する。
# coding=utf-8
import comtypes
from comtypes import CoCreateInstance
import comtypes.client
comtypes.client.GetModule('UIAutomationCore.dll')
from comtypes.gen.UIAutomationClient import *
uia = None
root_element = None
def init():
global uia, root_element
uia = CoCreateInstance(CUIAutomation._reg_clsid_, interface=IUIAutomation, clsctx=comtypes.CLSCTX_INPROC_SERVER|comtypes.CLSCTX_INPROC_HANDLER|comtypes.CLSCTX_LOCAL_SERVER|comtypes.CLSCTX_REMOTE_SERVER)
root_element = uia.GetRootElement()
# Get elements by condition
def get(base_element, condition, scope=TreeScope_Subtree):
elements = base_element.FindAll(scope, condition)
return [ elements.GetElement(i) for i in range(elements.Length) ]
if __name__ == '__main__':
init()
input('Open "//i-o.io/list.php" on Chrome then hit Enter key')
condition = uia.CreatePropertyCondition(UIA_NamePropertyId, "クリップリスト 1~50件目 - 鉄ロケハン - Google Chrome")
window = root_element.FindFirst(TreeScope_Children, condition)
condition = uia.CreatePropertyCondition(UIA_LocalizedControlTypePropertyId, "表")
table = get(window, condition)[0]
num_rows = table.GetCurrentPropertyValue(UIA_GridRowCountPropertyId)
num_columns = table.GetCurrentPropertyValue(UIA_GridColumnCountPropertyId)
values = [
table.GetCurrentPropertyValue(UIA_IsGridPatternAvailablePropertyId),
table.GetCurrentPropertyValue(UIA_IsTablePatternAvailablePropertyId),
num_rows,
num_columns,
]
print('\nGridPatternAvailableProperty: {}\nTablePatternAvailableProperty: {}\nGrid.RowCount: {} x Grid.ColumnCount: {}\n'.format(*values))
condition = uia.CreatePropertyCondition(UIA_IsEnabledPropertyId, True)
rows = get(table, condition, TreeScope_Children)
for i in range(num_rows):
cols = get(rows[i], condition, TreeScope_Children)
for j in range(num_columns):
col = cols[j]
values = [
col.GetCurrentPropertyValue(UIA_IsGridItemPatternAvailablePropertyId),
col.GetCurrentPropertyValue(UIA_IsTableItemPatternAvailablePropertyId),
col.GetCurrentPropertyValue(UIA_GridItemRowPropertyId),
col.GetCurrentPropertyValue(UIA_GridItemColumnPropertyId),
col.GetCurrentPropertyValue(UIA_GridItemRowSpanPropertyId),
col.GetCurrentPropertyValue(UIA_GridItemColumnSpanPropertyId),
col.CurrentName,
]
print('\nGridItemPatternAvailableProperty: {}\nTableItemPatternAvailableProperty: {}\nGridItem.Row: {}\nGridItem.Column: {}\nGridItem.RowSpan: {}\nGridItem.ColumnSpan: {}\nName: {}\n'.format(*values))
2020/02/09 11:41