Sasha
Sasha

Reputation: 6039

A problem with downloading a file with Python

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

Upvotes: 3

Views: 1160

Answers (4)

Robert Harvey
Robert Harvey

Reputation: 180777

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.

Upvotes: 1

Aaron Digulla
Aaron Digulla

Reputation: 328556

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.

Upvotes: 1

Robert Harvey
Robert Harvey

Reputation: 180777

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

Upvotes: 0

Robert Harvey
Robert Harvey

Reputation: 180777

Try this:

from watsup.winGuiAuto import *
optDialog = findTopWindow(wantedText="File Download")
SaveButton = findControl(optDialog, wantedClass="Button", wantedText="Submit")
clickButton(SaveButton) 

Upvotes: 0

Related Questions