pywinauto常见问题
1、应用之间切换如果要操作多个windows桌面应用,需要在多个应用之间切换,只需要app.top_window().set_focus()即可完成切换,注意切换后需要加延时,以免后续定位控件位置不准确。如果切换后立即用print_control_identifiers()打印APP控件,可以看到控件位置与最终位置不一致。app1 = Application(backend=Backend_Typ
1、应用之间切换
如果要操作多个windows桌面应用,需要在多个应用之间切换,只需要app.top_window().set_focus()即可完成切换,注意切换后需要加延时,以免后续定位控件位置不准确。如果切换后立即用print_control_identifiers()打印APP控件,可以看到控件位置与最终位置不一致。
app1 = Application(backend=Backend_Type).start(path1)
app1.top_window().set_focus()
app1.top_window().print_control_identifiers()
time.sleep(1)
app1.top_window().print_control_identifiers()
app2 = Application(backend=Backend_Type).start(path2)
app2.top_window().set_focus()
time.sleep(1)
2、如何知道控件支持哪些方法
获取控件后通过XXX.wrapper_object()或者type(XXX)获取控件类型,然后在接口文档中查找控件支持的操作。注意有些操作不一定能返回预期的结果。
例如对于SecureCRT应用,如果想尝试对SessionManager里面的Device1进行双击,以进行配置好的连接(可以是Telnet、SSH2、Serial各种)
1、UIA模式
app = Application(backend='uia').connect(path = 'C:/Program Files/VanDyke Software/Clients/SecureCRT.exe')
app.top_window().print_control_identifiers()
child_window(title="Session Manager", auto_id="33216", control_type="Pane")
| |
| | Pane - '' (L0, T99, R199, B124)
| | ['Pane2']
| | child_window(auto_id="135", control_type="Pane")
| | |
........................................................................................
| |
| | TreeView - '' (L1, T125, R198, B482)
| | ['TreeView']
| | child_window(auto_id="1036", control_type="Tree")
| | |
| | | ScrollBar - '垂直滚动条' (L180, T126, R197, B464)
| | | ['垂直滚动条', '垂直滚动条ScrollBar', 'ScrollBar', 'ScrollBar0', 'ScrollBar1', '垂直滚动条0', '垂直滚动条1', '垂直滚动条ScrollBar0', '垂直滚动条ScrollBar1']
| | | child_window(title="垂直滚动条", auto_id="NonClientVerticalScrollBar", control_type="ScrollBar")
| | | |
........................................................................................
| | |
| | | ScrollBar - '水平滚动条' (L2, T464, R180, B481)
| | | ['水平滚动条ScrollBar', '水平滚动条', 'ScrollBar2']
| | | child_window(title="水平滚动条", auto_id="NonClientHorizontalScrollBar", control_type="ScrollBar")
| | | |
........................................................................................
| | | Thumb - '' (L180, T464, R197, B481)
| | | ['Thumb3']
| | |
| | | TreeItem - 'Sessions' (L0, T0, R0, B0)
| | | ['TreeItem', 'SessionsTreeItem', 'Sessions', 'TreeItem0', 'TreeItem1']
| | | child_window(title="Sessions", control_type="TreeItem")
| | | |
| | | | TreeItem - '0local' (L0, T0, R0, B0)
| | | | ['TreeItem2', '0localTreeItem', '0local']
| | | | child_window(title="0local", control_type="TreeItem")
| | | |
| | | | TreeItem - 'Device1' (L62, T126, R111, B146)
| | | | ['TreeItem59', 'Device1TreeItem', 'Device1']
| | | | child_window(title="Device1", control_type="TreeItem")
| | | |
| | | | TreeItem - 'Device2' (L62, T146, R111, B166)
| | | | ['TreeItem60', 'Device2TreeItem', 'Device2']
| | | | child_window(title="Device2", control_type="TreeItem")
| | | |
可以看到界面上只有一个Device1,可以直接用魔法属性访问。通过wrapper_object()可以知道Device1的属性为uia_controls.TreeItemWrapper,查找帮助文档可知其没有双击方法,但是继承自pywinauto.controls.uiawrapper.UIAWrapper,也没有查到双击方法,再向上查看继承自pywinauto.base_wrapper.BaseWrapper,可以通过click_input(double= True)方法双击该Session。
app.top_window()['Device1'].print_control_identifiers()
Control Identifiers:
TreeItem - 'Device1' (L62, T166, R111, B186)
['TreeItem', 'Device1TreeItem', 'Device1']
child_window(title="Device1", control_type="TreeItem")
app.top_window()['Device1'].wrapper_object()
<uia_controls.TreeItemWrapper - 'Device1', TreeItem, -4503959006078651461>
app.top_window()['Device1'].click_input(double= True)
2、win32模式
app = Application(backend='win32').connect(path = 'C:/Program Files/VanDyke Software/Clients/SecureCRT.exe')
app.top_window().print_control_identifiers()
| Afx:ControlBar:3fee0000:8:10003:10 - 'Session Manager' (L0, T72, R199, B483)
| ['Afx:ControlBar:3fee0000:8:10003:10', 'Session Manager', 'Session ManagerAfx:ControlBar:3fee0000:8:10003:10', 'Afx:ControlBar:3fee0000:8:10003:100', 'Afx:ControlBar:3fee0000:8:10003:101']
| child_window(title="Session Manager", class_name="Afx:ControlBar:3fee0000:8:10003:10")
| |
| | Toolbar - '' (L0, T99, R199, B124)
| | ['Toolbar', 'not connected - SecureCRTToolbar', 'Toolbar0', 'Toolbar1']
| | child_window(class_name="Afx:ToolBar:3fee0000:8:10003:10")
| |
| | TreeView - '' (L1, T125, R198, B482)
| | ['TreeView', 'not connected - SecureCRTTreeView']
| | child_window(class_name="SysTreeView32")
...................................................................................................................
| Toolbar - 'Menu Bar' (L0, T22, R1366, B47)
| ['Toolbar2', 'Menu BarToolbar', 'Menu Bar']
| child_window(title="Menu Bar", class_name="Afx:ToolBar:3fee0000:8:10003:10")
可以看到WIN32访问模式打印出来的控件很少,似乎无法对Device1进行点击,通过分析TreeView为Session Manager部分,其类型为common_controls.TreeViewWrapper,根据接口文档尝试对TreeView进行访问,发现item_count()、print_items()、texts()、tree_root()、get_properties()、get_item()等方法都能对TreeView里面的item进行访问。
通过item = tree.get_item(path="\Sessions\Device1") ,或者item=tree.tree_root().children()[XX]都能访问到Session。
app.top_window()['TreeView'].wrapper_object()
<common_controls.TreeViewWrapper - '', TreeView, 134924>
app.top_window()['TreeView'].print_control_identifiers()
Control Identifiers:
TreeView - '' (L1, T125, R198, B482)
['TreeView']
child_window(class_name="SysTreeView32")
tree = app.top_window()['TreeView']
tree.item_count()
XX
tree.print_items()
'\nSessions\n 0local\n Device1\n Device2\n Device3\n Device4\n '
tree.texts()
['', 'Device1', 'Device2', 'Device3', 'Device4']
tree.tree_root()
<pywinauto.controls.common_controls._treeview_element object at 0x000000000C276668>
tree.tree_root().children()
[<pywinauto.controls.common_controls._treeview_element object at 0x000000000C276F28>, <pywinauto.controls.common_controls._treeview_element object at 0x000000000C276DD8>, <pywinauto.controls.common_controls._treeview_element object at 0x000000000C276B00>, <pywinauto.controls.common_controls._treeview_element object at 0x000000000C276D68>, <pywinauto.controls.common_controls._treeview_element object at 0x000000000C276828>]
tree.get_properties()
{'class_name': 'SysTreeView32', 'friendly_class_name': 'TreeView', 'texts': ['', 'Sessions', '0local', 'Device1', 'Device2', 'Device3', 'Device4'], 'control_id': 1036, 'rectangle': <RECT L1, T125, R198, B482>, 'is_visible': True, 'is_enabled': True, 'control_count': 0, 'style': 1353783343, 'exstyle': 0, 'user_data': 0, 'context_help_id': 0, 'fonts': [<LOGFONTW '微软雅黑' -12>], 'client_rects': [<RECT L0, T0, R178, B338>], 'is_unicode': True, 'menu_items': [], 'automation_id': '', 'item_count': XX}
item = tree.get_item(path="\Sessions\Device1") ,或者item=tree.tree_root().children()[XX]
对于item类型为pywinauto.controls.common_controls._treeview_element,通过item.click_input(double=True)可以进行双击,通过item.click(double=True)不能进行双击。
item.ensure_visible()
item.click_input(double=True)
type(item)
<class 'pywinauto.controls.common_controls._treeview_element'>
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
所有评论(0)