0

How can i right click the app's icon in the systemtray and select one of the popup menuitem using pywinauto?

I have my app on the Windows Desktop systemtray,which can't be loaded using the .exe file. So i have to right click on the systemtray icon and select one of the popup menu item in order to get the app's GUI.I was trying to achieve this using pywinauto using python 64 bit.

Here is my code.

app = Application(backend="uia").connect(path="explorer")
sys_tray = app.window(class_name="Shell_TrayWnd")
loc = sys_tray.child_window(title='App name').click()

This is changing the mouse position to the required App's icon,but its not right clicking on that and i want to select one menu item from that pop up also. how can i get this?

Peter Sobot
  • 2,476
  • 21
  • 20
ss333iiii
  • 35
  • 1
  • 7

2 Answers2

1

There is method .click_input(button="right") which moves real cursor and performs real click. In your case it would look so (on Windows 10 version 1803):

#from __future__ import print_function
from pywinauto import Desktop

d = Desktop(backend='uia')
#d.Taskbar.dump_tree()
main_tray_toolbar = d.Taskbar.child_window(title="User Promoted Notification Area", control_type="ToolBar")
#print(main_tray_toolbar.texts())

icon = main_tray_toolbar.child_window(title_re="Cisco AnyConnect Secure Mobility Client.*", control_type="Button")
icon.click_input(button="right")

#d.ContextMenu.dump_tree()
d.ContextMenu.wait('visible', timeout=10) # flexibly wait up to 10 sec.
d.ContextMenu.child_window(title="About", control_type="MenuItem").invoke()

Helpful debug prints are commented (all .child_window specifications have been just copied from dump_tree() output). There is also method d.windows() that is available for master branch only (pywinauto 0.6.6 is coming in nearest 2 weeks).

Vasily Ryabov
  • 9,386
  • 6
  • 25
  • 78
  • Hi,Vasily..It worked fine for me in one system.. on the other system i am getting the error as below: File "C:\Users\isreedex\AppData\Local\Programs\Python\Python37\lib\site-packages\pywinauto\application.py", line 246, in __resolve_control criteria) File "C:\Users\isreedex\AppData\Local\Programs\Python\Python37\lib\site-packages\pywinauto\timings.py", line 453, in wait_until_passes raise errpywinauto.timings.TimeoutError During handling of the above exception, another exception occurred: pywinauto.findbestmatch.MatchError: – ss333iiii Feb 07 '19 at 05:37
  • Which line exactly fails? If waiting for context menu failed, you have to use `d.ContextMenu.wait('visible', timeout=10)` (or more than 10 sec.) because default timeout is 5 sec. – Vasily Ryabov Feb 07 '19 at 15:03
  • If this is for `icon`, the problem might be a different title on the second system. – Vasily Ryabov Feb 07 '19 at 15:05
  • By the way, this code shouldn't work on Win8.1 or earlier. Some early Win10 versions have another toolbar structure. In this case it's possible to use old experimental module `pywinauto.taskbar` (need `from pywinauto import taskbar`). This module uses "win32" backend. Sample code can be found in unit tests: [pywinauto/unittests/test_taskbar.py#L158](https://github.com/pywinauto/pywinauto/blob/master/pywinauto/unittests/test_taskbar.py#L158) – Vasily Ryabov Feb 07 '19 at 15:11
  • ok. if am going witrh Desktop module, if the app's GUI is launched, how can i access the elements in the GUI? you have any document for this type of cases? – ss333iiii Feb 08 '19 at 07:18
  • It works the same way as for `Application` object but there is no binding to particular process. See [Getting Started Guide -> Entry Points for Automation](https://pywinauto.readthedocs.io/en/latest/getting_started.html#entry-points-for-automation). There are some [HowTo's](https://pywinauto.readthedocs.io/en/latest/HowTo.html) as well. – Vasily Ryabov Feb 08 '19 at 14:39
  • Method `.windows()` to list top level wrappers has been just implemented in `Desktop` class so pywinauto==0.6.5 doesn't have it, 0.6.6 is coming next week. – Vasily Ryabov Feb 08 '19 at 14:40
  • Hi Vasily. Sorry for late reply. I could done that. But now i want to write a common function which will take the group element as an argument. When i write that, i am getting the error: raise MatchError(items = name_control_map.keys(), tofind = search_text) pywinauto.findbestmatch.MatchError: Could not find 'dumpoption' in 'dict_keys(['Reapply', 'ReapplyCheckBox', 'CheckBox] My code is : def capturedump_selection(dumpoption): print(dumpoption) dlg.dumpoption.select() . But when i call that function with the inspect name as an argument, it shows error. Why this happening? – ss333iiii Feb 20 '19 at 07:37
  • It's incorrect usage. Should be `dlg[dumoption].select()` or `getattr(dlg, dumpoption).select()`. Current code is equivalent to `dlg["dumpoption"].select()` which doesn't use variable value, but just its name. – Vasily Ryabov Feb 20 '19 at 12:29
  • Can you give a proper document of this library? – ss333iiii Feb 21 '19 at 11:21
  • Core concept is described here: https://pywinauto.readthedocs.io/en/latest/getting_started.html – Vasily Ryabov Feb 21 '19 at 21:40
  • Function `getattr` is a built-in Python function. – Vasily Ryabov Feb 21 '19 at 21:41
  • is it possible to read a value from text edit field using pywinauto? i tried this but it returning empty lists. my code is dlg.child_window(title="Launch HCI Commander...", auto_id="BTHCICommandBtn", control_type="Button").click_input() hci= self.dlg.child_window(title="HCI Commander", auto_id="BTHCIcommand", control_type="Window") hci.child_window(auto_id="HcitextBox1", control_type="Edit").type_keys('05fc') hci.Send.click_input() txt = hci.child_window(auto_id="ResponsetextBox2", control_type="Edit").texts() print(txt) – ss333iiii Feb 26 '19 at 17:01
  • There is no static value provided for that field – ss333iiii Feb 26 '19 at 17:07
  • Maybe `.get_value()` would help if it exists for this control. Or `.iface_value.GetValue()` otherwise. – Vasily Ryabov Feb 27 '19 at 09:58
  • If you could provide `Inspect.exe` screenshot for target field, it would be very helpful. Anyway this is another question. I would suggest to accept this answer if it resolves initial problem (gray check box under voting buttons should work for you) and ask another question with tag "pywinauto" and all the details. – Vasily Ryabov Feb 27 '19 at 10:01
  • Accepting correct answers is considered polite on StackOverflow. – Vasily Ryabov Feb 27 '19 at 10:01
1

Comment on Vasily's answer: because the names of the windows and toolbars are localized, the code will not work on systems whose locale is not English. I got this to work on a French Windows 10 system by replacing

main_tray_toolbar = d.Taskbar.child_window(title="User Promoted Notification Area", control_type="ToolBar")

with

main_tray_toolbar = d.window(class_name='Shell_TrayWnd').child_window(class_name='ToolbarWindow32', control_type="ToolBar")