来自ListBoxWrapper的pywinauto访问方法

时间:2017-06-30 10:30:44

标签: python pywinauto

我正在使用pywinauto在GUI应用程序上自动执行一些测试。有一个列表框,我需要检查一些数据。 ListBoxWrapper类具有以下方法:

ListBoxWrapper.GetItemFocus
ListBoxWrapper.ItemCount
ListBoxWrapper.ItemData
ListBoxWrapper.ItemTexts

https://pywinauto.readthedocs.io/en/latest/code/pywinauto.controls.win32_controls.html#pywinauto.controls.win32_controls.ListBoxWrapper

如何访问这些方法?

这是我到现在所拥有的: - 我创建了一个Application实例并用它来启动程序 - 我有一个列表框的WindowSpecification实例

listbox = programwindowspec.child_window(title="abcdefg", control_type="ListItem")

从这里我如何获得ListBoxWrapper类方法?

PS:我不是Python的OOP方面的专家,所以请耐心等待我。

编辑:我使用.children()方法为窗口上的所有控件获取包装器,然后从子控件中过滤掉列表框。

window = app.window(handle=w_handle)
for child in window.children():
    if 'List' in child._control_types:
        print(child)
        text = child.texts()
        print(text)

这符合我的目的。但我认为_control_types是一个'私有'类属性。可以直接从课外访问它吗?

1 个答案:

答案 0 :(得分:0)

您似乎使用了backend="uia",但您提供的文档链接适用于backend="win32"。这些后端有2种不同的包装器。这是correct docs for UIA List* related wrappers

control_type搜索条件中使用child_window(...)是正确的。对于WindowSpecification,您可以创建 ListItemWrapper ,以便:

list_item = programwindowspec.child_window(title="abcdefg", control_type="ListItem")
item_wrapper = list_item.wrapper_object()

# list all available attributes for a list item wrapper
print(dir(item_wrapper))

要创建 ListViewWrapper (对于UIA),您需要在WindowSpecification中使用control_type="List"control_type="DataGrid"(我们对这两种控件类型使用相同的包装器)。< / p>