如何右键单击一个文件夹并使用pywinauto从上下文菜单中选择? [英] How to right click on a folder and select from context menu using pywinauto?

查看:259
本文介绍了如何右键单击一个文件夹并使用pywinauto从上下文菜单中选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm writing automation tests for a cloud syncing desktop application. The problem I'm facing is that I cannot select a sub-folder from a window and I cannot select an option from context menu when right-click on a folder.

示例:

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时,找不到包含所有子文件夹的列表.

Also, when I look on SWAPY, I cannot find a list with all sub-folders.

我有Windows 10 x64,python 64位.python和cmd以管理员身份运行.我也曾在Windows 7 x86和x64上尝试过,但没有成功.

I have windows 10 x64, python 64bit. The python and cmd are running as administrator. I have also tried on Windows 7 x86 and x64 without success.

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

@Vasily Ryabov, can you help me with an example?

推荐答案

继续vasiliy ryabov的回答.可以使用pywinauto的最新UIA分支来完成.假设您打开了一个 Video 文件夹,然后尝试右键单击一个名为 My Home Videos 的子文件夹.当弹出上下文菜单时,我们只需单击 Open 菜单项:

To continue vasiliy ryabov's answer. It can be accomplished with the latest UIA branch of pywinauto. Let's say you have a Video folder opened and you try to right click on a sub-folder called My Home Videos. When a context menu is popped up we just click on Open menu item:

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()

这篇关于如何右键单击一个文件夹并使用pywinauto从上下文菜单中选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆