Некоторые моменты сложны, поэтому я подготовил код с комментариями о них.
from pywinauto import Application, Desktop
Application().start(r'mmc devmgmt.msc') # mmc.exe spawns a child process
app = Application(backend="uia").connect(path='mmc.exe') # connect to a child one
main = app.window(title='Device Manager')
#main.dump_tree()
# expand group of devices
usb_bus = main.child_window(title="Universal Serial Bus controllers", control_type="TreeItem")
usb_bus.expand()
#usb_bus.dump_tree()
# call context menu for the second USB Root Hub (found_index=1)
second_usb_root_hub = usb_bus.child_window(title="USB Root Hub", control_type="TreeItem", found_index=1)
second_usb_root_hub.click_input(button='right')
# Root of the Context menu is in the system process, so use global Desktop object.
#print(Desktop(backend="uia").windows()) # all top level windows in the system
context_menu = Desktop(backend="uia").window(title='Context')
#context_menu.dump_tree()
# Child menu items are in the mmc.exe but it doesn't matter if we have root menu object.
context_menu.child_window(title="Properties", control_type="MenuItem").invoke()
Вывод .dump_tree()
на промежуточных этапах предоставляет наиболее полезную информацию о дочерних элементах. Все спецификации child_window()
просто скопированы с выходов .dump_tree()
.
Идентификатор процесса элементов можно проверить в Inspect.exe
(можно найти в Windows SDK):