前言
pywinauto 操作 MenuItem 菜单项,以及子菜单项。
items() 获取子菜单项
以navicat 为例操作,获取menu菜单项
from pywinauto import Application
# 上海悠悠 wx:283340479
# blog:https://www.cnblogs.com/yoyoketang/
app = Application('uia').start(r"D:\navicat\navicat11.0\navicat.exe")
win = app.window(title_re="Navicat Premium")
menu = win['Menu']
print(menu.items())
运行结果
[<uia_controls.MenuItemWrapper - '文件', MenuItem, 5740354900026072187>, <uia_controls.MenuItemWrapper - '查看', MenuItem, 5740354900026072187>, <uia_controls.MenuItemWrapper - '收藏夹', MenuItem, 5740354900026072187>, <uia_controls.MenuItemWrapper - '工具', MenuItem, 5740354900026072187>, <uia_controls.MenuItemWrapper - '窗口', MenuItem, 5740354900026072187>, <uia_controls.MenuItemWrapper - '帮助', MenuItem, 5740354900026072187>]
获取文件子菜单
# 获取文件子菜单
f = win.child_window(title='文件')
print(f.items())
运行结果
[<uia_controls.MenuItemWrapper - '新建连接', MenuItem, 5740354900026072187>, <uia_controls.MenuItemWrapper - '打开连接', MenuItem, 5740354900026072187>, <uia_controls.MenuItemWrapper - '关闭连接', MenuItem, 5740354900026072187>, <uia_controls.MenuItemWrapper - '-', MenuItem, 5740354900026072187>, <uia_controls.MenuItemWrapper - '导出连接...', MenuItem, 5740354900026072187>, <uia_controls.MenuItemWrapper - '导入连接...', MenuItem, 5740354900026072187>, <uia_controls.MenuItemWrapper - '-', MenuItem, 5740354900026072187>, <uia_controls.MenuItemWrapper - '关闭', MenuItem, 5740354900026072187>, <uia_controls.MenuItemWrapper - '退出', MenuItem, 5740354900026072187>]
click_input() 选中菜单
以navicat 为例操作:文件-打开连接
from pywinauto import Application
# 上海悠悠 wx:283340479
# blog:https://www.cnblogs.com/yoyoketang/
app = Application('uia').start(r"D:\navicat\navicat11.0\navicat.exe")
win = app.window(title_re="Navicat Premium")
menu = win['Menu']
# 点开文件
menu.child_window(title='文件').click_input()
# 点文件->打开连接
menu.item_by_path("文件->打开连接").click_input()
选择子菜单
选择子菜单有2种方式
- item_by_path 通过路径,如:文件->打开连接
- item_by_index(n) 通过索引
通过索引选择子菜单示例
from pywinauto import Application
app = Application('uia').start(r"D:\navicat\navicat11.0\navicat.exe")
win = app.window(title_re="Navicat Premium")
menu = win['Menu']
menu.item_by_index(0).click_input()
标签:pywinauto,windows,win,app,navicat,Application,menu,菜单项
From: https://www.cnblogs.com/yoyoketang/p/17652860.html