4

I just installed pywinauto-0.4.2 for Python 2.7.8 (on a Windows 7 64 bit machine). I was trying an example cited on pywinauto

The source code for the program SaveFromFirefox.py (given in the above URL) is:

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

When I try to execute the file in the command prompt, it opens up the firefox browser and then opens the "Save As" dialog box but doesn't press the "Save" button.

Rather, it gives the following error message:

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']'

What is going wrong?

fredtantini
  • 15,966
  • 8
  • 49
  • 55
Arun
  • 2,222
  • 7
  • 43
  • 78
  • Presumably Firefox has changed since that sample was written, and the `Save` dialog no longer has a field named `FileNameEdit`. You will need to read the PyWinAuto docs to see how to find the right name for the field. – abarnert Dec 18 '14 at 10:18
  • I am currently using Firefox 34 browser – Arun Dec 18 '14 at 10:18
  • Sure, and the sample hasn't been updated since March 2013, at which point Firefox was on version 17 or so, so it's not at all implausible that the sample would be out of date. Again, you will need to read the docs, which will explain how to find the right name for the field you want. (And then file a bug against pywinauto to get the sample fixed.) – abarnert Dec 18 '14 at 10:19
  • For a hint: Notice that the error message shows you the list of control names that it searched for `'FileNameEdit'`. Look at the ones that look like they might be edit boxes for a filename, and see what each one does when you set its text. – abarnert Dec 18 '14 at 10:26
  • Thanks abarnert, I will read the "pywinauto" docs to get to the root of it. – Arun Dec 21 '14 at 04:52

1 Answers1

1

Just change

app.SaveAs.FileNameEdit.SetEditText(outputfilename)

to

app.SaveAs.Edit.SetEditText(outputfilename)

It works for me on Firefox 34.0.5. I also changed path to

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

This is on Win7 x64 + Python 2.6.6 (32-bit).

The main reason why it didn't work is changed standard Open/Save dialog in Win7 in comparison with WinXP. Probably this example was written on WinXP few years ago.

Vasily Ryabov
  • 9,386
  • 6
  • 25
  • 78
  • Thanks Vasily, let me try it and get back to you. – Arun Dec 21 '14 at 04:51
  • 1
    Vasily, I used "Sikuli" to automate the accepting of the dialog box which was inevitably popped up by the Firefox in spite of having used many "tricks" to try and subdue it. Now it works. – Arun Mar 10 '15 at 10:18