Search code examples
python-3.x32-bitpywinautowin32guiinspect.exe

The windows application automating using pywinauto does not detect elements inside a TreeView, despite the elements have there own characteristic


method of tree viewTree structure of spy++The application I am automating is a win32 supported backend application and using inspect.exe to detect the elements Below is my code trying to click on sales receipt element, on execution I get error

code:screenshot of the treeview in inspect.exe while application image in background

app = Application(backend="win32").connect(process=5468)
app.windows()
dlg = app['TFMenuG.UnicodeClass']
handle = dlg.child_window(control_id='UIA_ButtonControlTypeId (0xC350)').draw_outline()

error:

Traceback (most recent call last):
  File "c:\..\pythonDemo\notepad.py", line 62, in <module>
    handle = dlg.child_window(control_id='UIA_ButtonControlTypeId (0xC350)').draw_outline()
  File "C:\..\AppData\Local\Programs\Python\Python39-32\lib\site-packages\pywinauto\application.py", line 379, in __getattribute__
    ctrls = self.__resolve_control(self.criteria)
  File "C:\..\AppData\Local\Programs\Python\Python39-32\lib\site-packages\pywinauto\application.py", line 261, in __resolve_control
    raise e.original_exception
  File "C:\..\AppData\Local\Programs\Python\Python39-32\lib\site-packages\pywinauto\timings.py", line 436, in wait_until_passes
    func_val = func(*args, **kwargs)
  File "C:\..\AppData\Local\Programs\Python\Python39-32\lib\site-packages\pywinauto\application.py", line 222, in __get_ctrl
    ctrl = self.backend.generic_wrapper_class(findwindows.find_element(**ctrl_criteria))
  File "C:\..\AppData\Local\Programs\Python\Python39-32\lib\site-packages\pywinauto\findwindows.py", line 87, in find_element
    raise ElementNotFoundError(kwargs)
pywinauto.findwindows.ElementNotFoundError: {'control_id': 'UIA_ButtonControlTypeId (0xC350)', 'top_level_only': False, 'parent': <win32_element_info.HwndElementInfo - '', TFMenuG.UnicodeClass, 196780>, 'backend': 'win32'}

Please help me a way to identify the elements. I am doubting the elements are not recognised because of win32 backend


Solution

  • First, if you use Inspect.exe you must use Application(backend="uia"). If you want to check the application compatibility with older "win32" backend, you need Spy++ which is included into Visual Studio.

    Second control_id is integer ID from Spy++ and it can be inconsistent from run to run. I would recommend printing top level window texts by print([w.window_text() for w in app.windows()]) and use necessary text to identify top level window and dump child identifiers:

    app.window(title="Main Window Title").dump_tree() # or use title_re for regular expression
    app.window(title="Main Window Title").child_window(title="Sales Receipts", control_type="TreeItem").draw_outline().click_input()
    # or get .wrapper_object() and discover all available methods,
    # wrapper methods can be chained as above
    

    P.S. If Inspect.exe doesn't show property "NativeWindowHandle", it means the element is not visible to "win32" backend.


    EDIT1:

    Try this code for the "win32" TreeView which is not automatically detected as TreeViewWrapper:

    from pywinauto import Application
    from pywinauto.controls.common_controls import TreeViewWrapper
    
    app = Application(backend="win32").connect(class_name="TFMenuG.UnicodeClass")
    dlg = app['TFMenuG.UnicodeClass']
    handle = dlg.child_window(class_name='THTreeView.UnicodeClass').wrapper_object().handle
    tree_view = TreeViewWrapper(handle)
    print(dir(tree_view)) # list all available methods
    
    tree_view.get_item("Sales Receipts").expand()
    tree_view.get_item(r"Sales Receipts\Reports").click(where="text")
    

    When you see all available methods, try documented methods for "win32" TreeView: https://pywinauto.readthedocs.io/en/latest/code/pywinauto.controls.common_controls.html#pywinauto.controls.common_controls.TreeViewWrapper Please note that _treeview_element object returned by get_item(...) represents specific item without window handle, but it's usable.