Python 如何使用pywinauto右键单击文件夹并从上下文菜单中选择?

Python 如何使用pywinauto右键单击文件夹并从上下文菜单中选择?,python,automation,pywinauto,Python,Automation,Pywinauto,我正在为云同步桌面应用程序编写自动化测试。我面临的问题是,当右键单击文件夹时,我无法从窗口中选择子文件夹,也无法从关联菜单中选择选项 例如: import pywinauto pywinauto.Application().Start(r'explorer.exe') explorer = pywinauto.Application().Connect(path='explorer.exe') NewWindow = explorer.Window_(top_level_only=True,

我正在为云同步桌面应用程序编写自动化测试。我面临的问题是,当右键单击文件夹时,我无法从窗口中选择子文件夹,也无法从关联菜单中选择选项

例如:

import pywinauto

pywinauto.Application().Start(r'explorer.exe')
explorer = pywinauto.Application().Connect(path='explorer.exe')
NewWindow = explorer.Window_(top_level_only=True, active_only=True, class_name='CabinetWClass')
NewWindow.AddressBandRoot.ClickInput()
NewWindow.TypeKeys(r'Program Files{ENTER}', with_spaces=True, set_foreground=False)
ProgramFiles = explorer.Window_(top_level_only=True, active_only=True, title='Program Files', class_name='CabinetWClass')
explorer.WaitCPUUsageLower(threshold=5)
item = ProgramFiles.FolderView.GetItem('7-Zip')
item.EnsureVisible()
item.RightClickInput()
答复:

    item = ProgramFiles.FolderView.GetItem('7-Zip')
  File "C:\Python27\lib\site-packages\pywinauto\application.py", line 243, in __getattr__
    ctrls = _resolve_control(self.criteria)
  File "C:\Python27\lib\site-packages\pywinauto\application.py", line 758, in _resolve_control
    raise e.original_exception
pywinauto.findbestmatch.MatchError: Could not find 'FolderView' in '[u'', u'ShellView', u'ShellViewSHELLDLL_DefView', 'Progress', u'UIRibbonCommandBar', u'24', u'25', u'20', u'21', u'22', u'23', u'WorkerW', u'0', u'ScrollBar', u'4', u'8', 'TreeView', u'DirectUIHWND', u'Address band toolbarToolbar', u'SHELLDLL_DefView', u'Namespace Tree ControlNamespaceTreeControl', u'UniversalSearchBand', u'WorkerW1', u'WorkerW0', u'Program FilesShellTabWindowClass', u'Tree ViewTreeView', u'3', u'7', u'NamespaceTreeControl', u'NUIPane', u'Search Box', u'CtrlNotifySink0', u'CtrlNotifySink1', u'CtrlNotifySink2', u'CtrlNotifySink3', u'Navigation buttons', u'Program Files', u'Address Band Root', u'SeparatorBand2', u'Navigation buttonsToolbar', u'Up band toolbarToolbar', u'WorkerW2', u'DUIViewWndClassName', u'UIRibbonCommandBarDock', u'Namespace Tree Control', u'2', u'6', u'ShellTabWindowClass', u'ReBarAddress', 'Toolbar3', u'RibbonUIRibbonWorkPane', u'Toolbar1', u'Toolbar0', 'Toolbar5', 'Toolbar4', u'Up band toolbar', u'11', u'10', u'13', u'12', u'15', u'14', u'17', u'16', u'19', u'18', u'UIRibbonDockTopUIRibbonCommandBarDock', u'UIRibbonDockTop', u'DirectUIHWND1', u'DirectUIHWND0', u'DirectUIHWND3', u'DirectUIHWND2', u'Address: C:\\Program FilesToolbar', u'Address: C:\\Program Files', u'Breadcrumb Parent', u'SearchEditBoxWrapperClass', u'UpBand', u'CtrlNotifySink', u'TravelBand', u'1', u'5', u'9', 'Toolbar', 'ReBar', u'NetUIHWND', u'Address band toolbar', u'SeparatorBand0', u'SeparatorBand1', u'RibbonUIRibbonCommandBar', u'Ribbon2', u'Ribbon1', u'Ribbon0', 'Toolbar2', u'Tree View', u'UIRibbonWorkPane', u'ReBar0', u'ReBar1', 'ReBar2', u'SeparatorBand', u'Ribbon']'
此外,当我查看SWAPY时,无法找到包含所有子文件夹的列表

我有Windows10x64,python 64位。python和cmd以管理员身份运行。我也尝试过Windows7x86和x64,但没有成功

@Vasily Ryabov,你能帮我举个例子吗?

(警告:用于pywinauto的UIA分支的早期测试;将在pywinauto 0.6.0发布后替换答案)

如果您熟悉Git,只需克隆主repo,切换到UIA分支,使用
pip install comtypes安装comtypes
,然后通过
python setup.py install
安装pywinauto(UIA分支)

浏览器应与
app=Application(backend='uia')一起运行。启动(“Explorer.exe”)
。除了方法的PEP8名称之外,其他内容看起来几乎相同(
click_input
而不是
ClickInput
等等)

对于上下文菜单,您可以使用以下内容(因为MenuWrapper尚未准备好用于UIA)

ListViewWrapper是最近为UIA实现的。任何反馈都将不胜感激


编辑1: 我可以获取文件列表,因此(假设
apps/MFC_Samples
文件夹处于打开状态):

输出:

[u'x64', u'BCDialogMenu.exe', u'CmnCtrl1.exe', u'CmnCtrl2.exe', u'CmnCtrl3.exe',
u'CtrlTest.exe', u'mfc100u.dll', u'RebarTest.exe', u'RowList.exe', u'TrayMenu.exe']

稍后我将准备更详细的示例。

继续Vasily ryabov的回答。它可以通过pywinauto的最新UIA分支完成。假设您打开了一个
视频
文件夹,并尝试右键单击名为
我的家庭视频
的子文件夹。当弹出上下文菜单时,我们只需单击
Open
菜单项:

dsk = pywinauto.Desktop(backend='uia')
explorer = pywinauto.Application().Connect(path='explorer.exe')

# chain actions: set focus and right click after that
explorer.Video.MyHomeVideos.set_focus().click_input(button='right')

# Here we get to the popup menu
dsk.Context.Open.click_input()

我还尝试了print_control_identifiers(),但没有FolderViewpywinauto 0.5.4不支持文件夹列表视图。但GitHub上的UIA分支能够处理这个问题。我现在没有关于
explorer.exe
的例子。但它在路线图上。如果您希望在0.6.0版本之前尝试新功能,请告诉我。没有0.6.0的确切日期,但我希望在今年夏天完成。嘿,瓦西里,谢谢你的回复。我真的很想在发布前测试和尝试新功能。我们如何继续?pywinauto 0.6.0已经发布。包含正在工作的
explorer.exe
示例。在运行python setup.py安装后,我收到以下错误:文件“C:\pywinauto-64-UIA\pywinauto\controls\common_controls.py”,第923行,在TreeViewRapper UIA_TreeControlTypeId]NameError:名称“UIA_TreeControlTypeId”未定义我已卸载python并删除所有依赖项,然后安装python 64位、comtypes、pillow和python for Windows扩展(我有一个错误:没有名为win32api的模块)。您可以使用
C:\Python27\u x64\Scripts\pip.exe安装pypiwin32扩展,其他用户也可以使用此功能。从未遇到过第一个问题。我需要更多信息来复制它。另一种访问上下文菜单的方法:
explorer.PopupMenu[“打开”]。设置焦点()。单击输入()
而不是
dsk.context.Open。单击输入()
[u'x64', u'BCDialogMenu.exe', u'CmnCtrl1.exe', u'CmnCtrl2.exe', u'CmnCtrl3.exe',
u'CtrlTest.exe', u'mfc100u.dll', u'RebarTest.exe', u'RowList.exe', u'TrayMenu.exe']
dsk = pywinauto.Desktop(backend='uia')
explorer = pywinauto.Application().Connect(path='explorer.exe')

# chain actions: set focus and right click after that
explorer.Video.MyHomeVideos.set_focus().click_input(button='right')

# Here we get to the popup menu
dsk.Context.Open.click_input()