UI Spy (Spy ++, SpyXX or analogs) can "browse" all the elements of the application. See the whole tree with the names of windows, handles, etc. Here is an example: Spy ui

How to get inside, knowing parental handles? How to get the names of nested windows, buttons and other entities. I want to implement a similar functionality, as in the screenshot above. I tried to get to the names of nested windows:

import win32gui parent_handle = 131588 child_handles = [] child_names = [] def all_ok(hwnd, param): child_handles.append(hwnd) child_names.append(win32gui.GetWindowText(hwnd)) win32gui.EnumChildWindows(parent_handle, all_ok, None) print(child_handles) print(child_names) 

But I get the error:

  Traceback (most recent call last): File "C:/Users/ПК/PycharmProjects/TESTING/2.py", line 12, in <module> win32gui.EnumChildWindows(parent_handle, all_ok, None) pywintypes.error: (126, 'EnumChildWindows', 'Не найден указанный модуль.') 

Google did not help: (

Update. How to get the name of an element if it has a handle of the form 42 197570 2 2 18 (nested element, with spaces)?

Update 2. What's wrong. Let's figure it out. In the picture the main window: main window

Indeed, handle = 65922.

 import win32gui hwnd = 65922 print(win32gui.GetWindowText(hwnd)) 

Output: PureVPN - Secure Virtual Private Network Connection (Administrator)

But then we see a child window (a button with no name, but we must also somehow access it, in order to later get its child windows): child window

 import win32gui hwnd = 65922 # Parent window nIDDlgItem = 27151449 # Что здесь должно быть ? h = win32gui.GetDlgItem(hwnd, nIDDlgItem) print(h) 

All this ends with pain:

 Traceback (most recent call last): File "C:/Users/ПК/PycharmProjects/TESTING/3.py", line 5, in <module> h = win32gui.GetDlgItem(hwnd, nIDDlgItem) pywintypes.error: (1421, 'GetDlgItem', 'Не найден идентификатор элемента управления.') 
  • And if you move 2.py for example in С:/ , and execute there? - mega
  • Understood what the problem is. This is not because of Russian characters on the way. Although, the name of the administrator will change, thank you :) - Alexander
  • The problem is in the handle of children. If the parent handle 131588, for example, then the kids 131588 2, 131588 38 and so on. (with space handle). And applications that are scanned by this code have child handles normal, without spaces. How can I solve this problem? - Alexander
  • one
    Most likely, this is not a handle with a space; this is the ID child window indicated by a space. The handle of such a child window can be obtained via a call to GetDlgItem . Transfer the value after the space to hDlg , and the value before the space in hDlg - mega
  • Well, 27151449 - it may well be a handle to the child window, and 2252 - you have indicated above that this is a PID . - mega

0