这个功能在windows上测试安装卸载时,有时会用到,网上查到的两种语言的版本如下:
C#版:
Shell shell = new Shell(); Folder folder = shell.NameSpace(Path.GetDirectoryName(appPath)); FolderItem app = folder.ParseName(Path.GetFileName(appPath)); string sVerb = isLock ? "锁定到任务栏(&K)" : "从任务栏脱离(&K)"; foreach (FolderItemVerb Fib in app.Verbs()) { if (Fib.Name == sVerb) { Fib.DoIt(); return true; } } return false;
Public Shared Function LockApp(isLock As Boolean, appPath As String) As Boolean Dim shell As New Shell() Dim folder As Folder = shell.[NameSpace](Path.GetDirectoryName(appPath)) Dim app As FolderItem = folder.ParseName(Path.GetFileName(appPath)) Dim sVerb As String = If(isLock, "锁定(&K)", "脱离(&K)") For Each Fib As FolderItemVerb In app.Verbs() If Fib.Name = sVerb Then Fib.DoIt() Return True End If Next Return False End Function
接下来,就是要把上面的代码如何转化为Python了,此处使用到了Windows接口,Python中调用windows接口,可以使用win32com
代码如下:
def DeleteQuickLaunchOnTaskBar(lnkName): objShell = win32com.client.Dispatch("Shell.Application") taskbarPath = os.path.join(os.environ["appdata"], r'Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar') lnkName = DesktopCommon.ToUnicode(lnkName) objFolder = objShell.NameSpace(taskbarPath) desktopItems = objFolder.Items() for item in desktopItems: if DesktopCommon.ToUnicode(item.Name) == lnkName: verbs = item.Verbs() for verb in verbs: if DesktopCommon.ToUnicode(verb.Name) == u"从任务栏脱离(&K)" or DesktopCommon.ToUnicode(verb.Name) == u"Unpin from Tas&kbar": verb.DoIt()Python版本的图标操作就OK了
欢迎关注“搜狗测试”公众号,每天一篇测试相关的文章与您分享,共同讨论软件测试的技术与发展
转载请注明:http://blog.csdn.net/sogouauto
原文地址:http://blog.csdn.net/sogouauto/article/details/44082445