Search code examples
python-3.xwindows-10ui-automationpywinauto

pywinuto - click_input() function clicks on random node of tree view


I'm trying to automate the mmc (snap in console) type desktop app. Where I need to expand the tree.

Try 1 - But when I do use expand() function it generates error popup which says that "the child nodes does not exists". After this script simply stops with no error message on console unless ok button is clicked on error popup. This I suspect because when tree node expanded it triggers some event which brings child nodes under it and somehow pywinauto is not getting Childs of this node. Please note that this error popup does not come up when the step is done manually.

Try 2 - When I tried with select() then too same behavior observed as above. It generated same error popup.

Try 3 - I tried click() and click_input() functions and it clicks on random tree node but not on the tree node on which it is called.

The all above trials are of my application which is not public.

For reproducing this issue I have tried it on common application available on windows OS. And we can see that the 3rd is still reproducible.

Reference code -

import time
from pywinauto.application import Application
from pywinauto import Desktop

app = Application().start(r'cmd.exe /c "C:\Windows\system32\perfmon.msc"', wait_for_idle=False)
app = Application(backend="win32").connect(title='Performance Monitor', timeout=10)
main_wind = app.window(best_match='MMCMainFrame', top_level_only=False)
console_wind = main_wind.child_window(best_match="MDIClients").child_window(best_match='MMCChildFrm').child_window(class_name="MMCViewWindow")

tree = console_wind.TreeView  # print_control_identifiers()

children = tree.get_item(["Performance","Data Collector Sets"]).expand().get_child("System").expand().get_child('System Diagnostics')
print(children.text())

# below line will select the System Diagnostics
children.select()
time.sleep(4)

# Below line should click on System Diagnostics but it does NOT and same happens for click() function
children.click_input()

Any help will be really helpful in knowing why this click() and click_input() clicks on random tree node and Why expand() and select() method generates the non functional error popup?

Please mention if there is concrete workaround to this.

versions - Windows OS 10, build 20H2
Python 3.10.4 comtypes 1.1.11
pip 22.0.4
pywin32 303
pywinauto 0.6.8
setuptools 61.2.0
six 1.16.0
wheel 0.37.1

TIA..

I have referenced some stack overflow and github issue tracker as for this problem as below but nothing works.

Treeview problem
select() for TreeView items (and similar) leads error if this selection calls other dialogs etc
Python: Click by coordinate inside a window


Solution

  • This was all because of incorrect backend API used for snap in control application. Unfortunately my POC I did on win32 API where it worked because I was doing some initial steps manually hence the problem I was facing(explained in try1) was not there and everything was working perfectly fine.

    As soon as I switched the backend to UIA it gave me different identifiers for the controls that I used previously. Now I used this identifiers and started using UIA documentation and everything started working smoothly.

    Also in my investigation there is no proper way to identify the backend API for the desktop application unless you try both and figure out which works for you.

    Suggestion to readers - If you are using win32 API as backend there are different api methods available for that in the documentation. and for UIA backend different. read documentation carefully.