【问题标题】:Error saving Firefox webpage using pywinauto in Python在 Python 中使用 pywinauto 保存 Firefox 网页时出错
【发布时间】:2014-12-18 10:13:47
【问题描述】:

我刚刚为 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 files\Mozilla Firefox\Firefox.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:\Users\arun_m\Desktop>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:\Python27\lib\site-packages\pywinauto\application.py", line 229, in __getattr__
    ctrls = _resolve_control(self.criteria)
  File "C:\Python27\lib\site-packages\pywinauto\application.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_backup\\Work\\Automation Factory\\Selenium\\Sikuli', 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_backup\\Work\\Automation Factory\\Selenium\\SikuliToolbar', 'Toolbar0', u'FloatNotifySink1', u'DirectUIHWND1', u'DirectUIHWND0', u'Cancel', u'D
irectUIHWND2']'

出了什么问题?

【问题讨论】:

  • 可能 Firefox 在编写该示例后发生了变化,Save 对话框不再有名为 FileNameEdit 的字段。您需要阅读 PyWinAuto 文档以了解如何为该字段找到正确的名称。
  • 我目前使用的是 Firefox 34 浏览器
  • 当然,该示例自 2013 年 3 月以来一直没有更新,当时 Firefox 的版本为 17 左右,因此该示例已过时并非不可信。同样,您需要阅读文档,其中将解释如何为您想要的字段找到正确的名称。 (然后针对 pywinauto 提交错误以修复示例。)
  • 提示:请注意,错误消息向您显示了它搜索'FileNameEdit' 的控件名称列表。查看那些看起来可能是文件名编辑框的那些,并查看每个在设置文本时的作用。
  • 感谢 abarnert,我将阅读“pywinauto”文档以了解其根源。

标签: python firefox pywinauto


【解决方案1】:

只是改变

app.SaveAs.FileNameEdit.SetEditText(outputfilename)

app.SaveAs.Edit.SetEditText(outputfilename)

它适用于 Firefox 34.0.5。我也改变了路径

app = application.Application().start_(
r"C:\Program Files (x86)\Mozilla Firefox\firefox.exe %s"% web_addresss)

这是在 Win7 x64 + Python 2.6.6(32 位)上。

它不起作用的主要原因是与 WinXP 相比,Win7 中的标准打开/保存对话框发生了变化。可能这个例子是几年前在 WinXP 上写的。

【讨论】:

  • 谢谢 Vasily,让我试试,然后回复你。
  • Vasily,我使​​用“Sikuli”来自动接受对话框,尽管我使用了许多“技巧”来尝试制服它,但 Firefox 不可避免地会弹出该对话框。现在可以了。
猜你喜欢
  • 2016-10-16
  • 2021-05-06
  • 2012-06-13
  • 1970-01-01
  • 1970-01-01
  • 2013-09-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-17
相关资源
最近更新 更多