views:

379

answers:

4

Hello,

I try to automatically download a file by clicking on a link on the webpage. After clicking on the link, I get the 'File Download' Window dialog with 'Open', 'Save' and 'Cancel' buttons. I would like to click the Save button.

I use watsup library in the following way:

from watsup.winGuiAuto import *

optDialog = findTopWindow(wantedText="File Download")

SaveButton = findControl(optDialog,wantedClass="Button", wantedText="Save")

clickButton(SaveButton)

For some reason it does not work. The interesting thing is that exactly the same code works perfectly to click on 'Cancel' button, however it refuses to work with 'Save' or 'Open'.

Anybody knows what I should do?

Thank you very much, Sasha

A: 

Try this:

from watsup.winGuiAuto import *
optDialog = findTopWindow(wantedText="File Download")
SaveButton = findControl(optDialog, wantedClass="Button", wantedText="Submit")
clickButton(SaveButton)
Robert Harvey
Since the text of the button is `Save`, I do not think this will work.
Alan Haggai Alavi
Except that "Save" doesn't work either. Do you know why?
Robert Harvey
Sasha
Does the button you are clicking require the focus before SaveButton will work? Is the button grayed out (disabled) at the moment SaveButton executes, because the dialog is waiting for some input to validate?
Robert Harvey
No, it is not disabled. I can easily press on it with a mouse. I don't know if it requires focus. Suppose it does, how do I focus on it?
Sasha
A: 

It's possible that the save button is not always enabled. While it may look to your eye that it is, a program might see an initial state that you're missing. Check it's state and wait until it's enabled.

[EDIT] But it's possible that Robert is right and the dialog will just ignore you for security reasons. In this case, I suggest to use BeautifulSoup to parse the HTML, extract the URL and download the file in Python using the urllib2 module.

Aaron Digulla
As I wrote previously the button is a regular windows button and not a part of html page. While downloading file, you get 'File Download' window open, with regular Open, Save, Cancel buttons. After pressing Save, windows opens Save As window where you write the file name and press another Save button. However, I am still stuck on the first window...
Sasha
Updated my answer. But I guess Robert Harvey is right.
Aaron Digulla
A: 

Sasha,

The code at this link is supposed to work. It uses ctypes instead of watsup.winGuiAuto, and relies on win32 calls. Here is the code:

from ctypes import *
user32 = windll.user32

EnumWindowsProc = WINFUNCTYPE(c_int, c_int, c_int)

def GetHandles(title, parent=None):
'Returns handles to windows with matching titles'
hwnds = []
def EnumCB(hwnd, lparam, match=title.lower(), hwnds=hwnds):
title = c_buffer(' ' * 256)
user32.GetWindowTextA(hwnd, title, 255)
if title.value.lower() == match:
hwnds.append(hwnd)

if parent is not None:
user32.EnumChildWindows(parent, EnumWindowsProc(EnumCB), 0)
else:
user32.EnumWindows(EnumWindowsProc(EnumCB), 0)
return hwnds

Here's an example of calling it to click the Ok button on any window that has the title "Downloads properties" (most likely there are 0 or 1 such windows):

for handle in GetHandles('Downloads properties'):
for childHandle in GetHandles('ok', handle):
user32.SendMessageA(childHandle, 0x00F5, 0, 0) # 0x00F5 = BM_CLICK
Robert Harvey
Nop, the same symptom: Save button does not work, Cancel works perfectly.Here is the example (it is not the same page that I need, but it reproduces the same problem). Go to:http://www.winzip.com/downwz.htmClick on Download Evaluation and then try to click on Save button using Python...I would be very
Sasha
I would be very greatful if someone could help me with this...Thank you in advance.Sasha
Sasha
Sasha, does this work with a program other than Winzip? It is possible that the Winzip program is intercepting the Win32 call and discarding it.
Robert Harvey
+1  A: 

Sasha,

It is highly likely that the file dialog you refer to (the Security Warning file download dialog) will NOT respond to windows messages in this manner, for security reasons. The dialog is specifically designed to respond only to a user physically clicking on the OK button with his mouse. I think you will find that the Run button will not work this way either.

Robert Harvey