akkab
akkab

Reputation: 401

Handling Context Menu of the taskbar icon with pywinauto

I am trying to automate the exiting operation on one of the apps. The app's icon is located in the taskbar. I was successfull in opening that icon's context menu with the modified code that I have found on stackoverflow:

import pywinauto
from pywinauto.application import Application
import time

app= "Service is enabled."

app = Application(backend="uia").connect(path="explorer.exe")
st = app.window(class_name="Shell_TrayWnd")
t = st.child_window(title="Notification Chevron").wrapper_object()
t.click()

time.sleep(1)

list_box = Application(backend="uia").connect(class_name="NotifyIconOverflowWindow")
list_box_win = list_box.window(class_name="NotifyIconOverflowWindow")
list_box_win.wait('visible', timeout=30, retry_interval=3)

# time.sleep(1)

appOpened= list_box_win.child_window(title = app)
appOpened.click_input(button = "right")

After the execution of the code above I get to the point when the context menu is opened: enter image description here

The next thing that I want to do is to click on Exit, I have tried doing it by specifying the mouse click coordinates, but I have noticed that the position of the parent icon is changing from time to time. What I would like to do is to get the handle on the Exit button and send click automatically.

------Edit-------

The icon is located in the hidden icons

Upvotes: 1

Views: 1191

Answers (2)

Dev-Ops-eloper
Dev-Ops-eloper

Reputation: 1

In this case, a solution that worked for me was to check active windows using the Desktop utility from pywinauto:

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

import time

app_name= "Service is enabled."

app = Application(backend="uia").connect(path="explorer.exe")
st = app.window(class_name="Shell_TrayWnd")
t = st.child_window(title="Notification Chevron").wrapper_object()
t.click()

time.sleep(1)

list_box = Application(backend="uia").connect(class_name="NotifyIconOverflowWindow")
list_box_win = list_box.window(class_name="NotifyIconOverflowWindow")
list_box_win.wait('visible', timeout=30, retry_interval=3)

# time.sleep(1)

appOpened= list_box_win.child_window(title = app_name)
appOpened.click_input(button = "right")

time.sleep(1)

dlg = Desktop(backend="uia").Context
sub_win = dlg.child_window(title='title of option') 
sub_win.click_input(button='left')

Do note that the dlg = Desktop(backend="uia").Context line only works if there is a visible context menu. You can check by using the Desktop(backend="uia").windows() utility and see what windows are currently visible and from there try out the options.

Upvotes: 0

ArtyGator
ArtyGator

Reputation: 61

So you want to access to the right click context menu. As said in this answer, you can do something like :

listbox.PopupMenu["Exit"].set_focus().click_input()

Upvotes: 1

Related Questions