pywinauto FAQ

1. Switch between apps

If you want to operate multiple windows desktop applications, you need to switch between multiple applications, just need app.top_window().set_focus() to complete the switch, pay attention to the need to add a delay after the switch, so as to avoid the subsequent positioning control position is not accurate. If you print the APP control with print_control_identifiers() immediately after switching, you can see that the control position is inconsistent with the final position.

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. How to know which methods the control supports

After obtaining the control, obtain the control type through XXX.wrapper_object() or type(XXX), and then find the operations supported by the control in the interface document. Note that some operations may not return the expected results.

For example, for SecureCRT application, if you want to double-click Device1 in SessionManager to make a configured connection (it can be Telnet, SSH2, Serial)

1. UIA mode

 

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)
   | | | ['Vertical scroll bar','Vertical scroll bar ScrollBar','ScrollBar','ScrollBar0','ScrollBar1','Vertical scroll bar 0','Vertical scroll bar 1','Vertical scroll bar ScrollBar0', 'Vertical scroll bar ScrollBar1']
   | | | child_window(title="Vertical scroll bar", auto_id="NonClientVerticalScrollBar", control_type="ScrollBar")
   | | | | 
.............. ................................................. ........................
   | | | 
   | | | ScrollBar-'Horizontal Scroll Bar' (L2, T464, R180, B481)
   | | | [ 'Horizontal scroll bar ScrollBar','Horizontal scroll bar','ScrollBar2']
   | | | child_window(title="Horizontal scroll bar", 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")
   | | | | 
You can see that there is only one Device1 on the interface, which can be accessed directly with magic attributes. Through wrapper_object(), you can know that the attribute of Device1 is uia_controls.TreeItemWrapper. Finding the help document shows that it does not have a double-click method, but it inherits from pywinauto.controls.uiawrapper.UIAWrapper, and there is no double-click method. Then look up and inherit from pywinauto.base_wrapper. BaseWrapper, you can double-click the Session through the click_input(double=True) method.

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 mode

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")

 

It can be seen that there are few controls printed in the WIN32 access mode, and it seems that Device1 cannot be clicked. By analyzing the TreeView as the Session Manager part, its type is common_controls.TreeViewWrapper. According to the interface document, I try to access the TreeView and found item_count(), print_items (), texts(), tree_root(), get_properties(), get_item() and other methods can access the items in the TreeView.

Through item = tree.get_item(path="\Sessions\Device1"), or item=tree.tree_root().children()[XX], you can access 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]

 

For item type pywinauto.controls.common_controls._treeview_element, double-click can be done through item.click_input(double=True), and double-click can not be done through item.click(double=True).

item.ensure_visible()

item.click_input(double=True)

type(item)
<class 'pywinauto.controls.common_controls._treeview_element'>

 

Guess you like

Origin blog.csdn.net/bluewhu/article/details/107815326