1

I need to retrieve window handler of Context menu for Windows desktop application automation. I used win32gui module to find the window handler, but it seems it does not work with the context menu.

import win32gui

# works fine with normal window
print(hex(win32gui.FindWindow("Micro", None)))
# does not work with context menu
print(hex(win32gui.FindWindow("Context", None)))

1 Answer 1

2

I did manage with these functions as workaround. The firs retrieve the window handle based on title, and the second retrieve the handle by the class name. This second approach can be used to find the context menu. Also this class can be used with Robot framework.

import win32gui


class AppTopLevelWindowFinder:
    def find_window_handler_by_title(self, name):
        windows = []
        win32gui.EnumWindows(enumHandler, windows)
        for next_window in windows:
            if name in next_window[0]:
                return next_window[2]


    def find_window_handler_by_class(self, classname):
        windows = []
        win32gui.EnumWindows(enumHandler, windows)
        for next_window in windows:
            if classname in next_window[1]:
                return next_window[2]


def enumHandler(hwnd, lwindow):
    if win32gui.IsWindowVisible(hwnd):
        lwindow.append((win32gui.GetWindowText(hwnd), win32gui.GetClassName(hwnd), hex(hwnd)))


newobj = AppTopLevelWindowFinder()
print(newobj.find_window_handler_by_title('Incoming Call'))
print(newobj.find_window_handler_by_class('32768'))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.