使用Python中的pywinauto保存Firefox网页时出错
发布时间:2020-12-20 13:46:32 所属栏目:Python 来源:网络整理
导读:我刚刚为 Python 2.7.8安装了pywinauto-0.4.2(在Windows 7 64位机器上).我试着在 pywinauto上引用一个例子 SaveFromFirefox.py程序的源代码(在上面的URL中给出)是: from pywinauto.application import Applicationimport sysimport timeimport os.pathif le
我刚刚为
Python 2.7.8安装了pywinauto-0.4.2(在Windows 7 64位机器上).我试着在
pywinauto上引用一个例子
SaveFromFirefox.py程序的源代码(在上面的URL中给出)是: from pywinauto.application import Application import sys import time import os.path if len(sys.argv) < 2: print "please specify a web address to download" sys.exit() web_addresss = sys.argv[1] if len(sys.argv) > 2: outputfilename = sys.argv[2] else: outputfilename = web_addresss outputfilename = outputfilename.replace('/','') outputfilename = outputfilename.replace('','') outputfilename = outputfilename.replace(':','') # start IE with a start URL of what was passed in app = Application().start_( r"c:program filesMozilla FirefoxFirefox.exe %s"% web_addresss) # some pages are slow to open - so wait some seconds time.sleep(4) # mozilla is one of thos applications that use existing windows # if they exist (at least on my machine!) # so if we cannot find any window for that process # - find the actual process # - connect to it if app.windows_(): mozilla = app.window_(title_re = ".*Mozilla Firefox") else: app = Application().connect_(title_re = ".*Mozilla Firefox") mozilla = app.window_(title_re = ".*Mozilla Firefox") # ie doesn't define it's menus as Menu's but actually as a toolbar! print "No Menu's in FireFox:",mozilla.MenuItems() # File -> Save As mozilla.TypeKeys("%FA") #ie.Toolbar3.PressButton("File") app.SaveAs.FileNameEdit.SetEditText(outputfilename) app.SaveAs.Save.CloseClick() # if asked to overwrite say yes if app.SaveAs.Yes.Exists(): app.SaveAs.Yes.CloseClick() print "saved:",outputfilename # File close tab or close #(Firefox makes it easy for us having the same shortcut for both! mozilla.TypeKeys("%FC") 当我尝试在命令提示符下执行该文件时,它会打开firefox浏览器,然后打开“另存为”对话框,但不按“保存”按钮. 相反,它给出以下错误消息: C:Usersarun_mDesktop>python2 SaveFromFirefox.py www.google.com No Menu's in FireFox: [] Traceback (most recent call last): File "SaveFromFirefox.py",line 51,in <module> app.SaveAs.FileNameEdit.SetEditText(outputfilename) File "C:Python27libsite-packagespywinautoapplication.py",line 229,in __getattr__ ctrls = _resolve_control(self.criteria) File "C:Python27libsite-packagespywinautoapplication.py",line 795,in _resolve_control raise e.original_exception pywinauto.findbestmatch.MatchError: Could not find 'FileNameEdit' in '['',u'ShellView',u'Address: D:My_Dox_backupWorkAutomation FactorySeleniumSikuli',u'DUIViewWndClas sName','Toolbar4',u'ShellViewSHELLDLL_DefView',u'Namespace Tree Control',u'Breadcrumb Parent',u'Address Band Root',u'Tree ViewTreeView',u'UniversalSearchBand',u'SearchEditB oxWrapperClass','ComboBox2','Progress','ComboBox0','ComboBox1','Toolbar2',u'CtrlNotifySink',u'ScrollBar','20','21','22','23',u'FloatNotifySink0','ComboBox',u'FloatNot ifySink2',u'TravelBand',u'WorkerW','1','0','3','2','5','4','7','6',u'NamespaceTreeControl','8','12','TreeView','Toolbar1',u'CancelButton','Toolbar','11',u'Search Box','ReBar','Edit','Button',u'CtrlNotifySink0',u'CtrlNotifySink1',u'CtrlNotifySink2','Toolbar3','9','Button1','Button0',u'FloatNotifySink',u'DirectUIHWND3',u'&SaveBu tton',u'DirectUIHWND','10','13',u'CancelScrollBar','15','14','17','Button2','19','18',u'Tree View','16',u'SHELLDLL_DefView',u'Namespace Tree ControlNamespaceTreeContr ol',u'&Save',u'Address: D:My_Dox_backupWorkAutomation FactorySeleniumSikuliToolbar','Toolbar0',u'FloatNotifySink1',u'DirectUIHWND1',u'DirectUIHWND0',u'Cancel',u'D irectUIHWND2']' 出了什么问题? 解决方法
只是改变
app.SaveAs.FileNameEdit.SetEditText(outputfilename) 至 app.SaveAs.Edit.SetEditText(outputfilename) 它适用于Firefox 34.0.5.我也改变了路径 app = application.Application().start_( r"C:Program Files (x86)Mozilla Firefoxfirefox.exe %s"% web_addresss) 这是在Win7 x64 Python 2.6.6(32位)上. 与WinXP相比,Win7中的标准打开/保存对话框更改了它无法正常工作的主要原因.可能这个例子几年前写在WinXP上. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |