This first dialog is displayed if you click a button, or link that causes a file to get downloaded. I guess this may first dialog may or may not appear, depending on what security settings you have chosen.

This dialog appears when you try to download a file. Its basically asking the location where you want to save it.

The easiest solution is to disable the popup and download the file automatically.
- Internet Explorer I thought this would do it, but I was wrong, I still get File Download popup: Tools > Internet Options > Security > select zone (example: Internet) > Security levels for this zone > Custom level... > Downloads > File download > Automatic prompting for file downloads > Disable > OK > Yes > OK.
- Firefox > (Preferences... | Tools > Options) > Applications > Content Type (click one, example: MP3 Audio File) > change Always ask to Save File > OK. (Source: Using Firewatir with Ubuntu for Download Testing.)
- Chrome > (Preferences... | wrench > Options) > Under the Hood > Downloads > Ask where to save each file before downloading > uncheck.
- Safari on Mac does not even have the popup. On Windows: Edit > Preferences... > General > Always prompt before downloading > uncheck.
- Opera > (Preferences... | Menu > Settings > Preferences...) > Advanced > Downloads > MIME Type (click one, example: audio/mp3) > Edit... > Action > Save to disk > Do not ask for folder, but save directly to > Choose... > chose folder > OK > OK.
Hints
- Do not have any web page (including this one) or other app open with "File Download" in the title bar or you may get a hold of that window when, for example, calling ai.WinWait("File Download")
- If testing in irb you may have to put several commands (separated with semicolons) on a single line and run them together so that all window focusing/clicking happens without losing focus to your irb window
ai = WIN32OLE.new("AutoItX3.Control");ai.WinWait("File Download", "", 5);ai.ControlFocus("File Download", "", "&Save");sleep 1; ...
Solution 1 (Windows and Internet Explorer)
Try this as a solution:
prompt_message = "Do you want to open or save this file?"
window_title = "File Download"
save_dialog = WIN32OLE.new("AutoItX3.Control")
sleep 1
save_dialog_obtained = save_dialog.WinWaitActive(window_title,prompt_message, 25)
save_dialog.ControlFocus(window_title, prompt_message, "&Save")
sleep 1
save_dialog.Send("S")
save_dialog.ControlClick(window_title, prompt_message, "&Save")
save_dialog.WinSetTitle(window_title, prompt_message, "This is ForTesting" )
saveas_dialog_obtained = save_dialog.WinWait("Save As", "Save&in", 5)
sleep 1
path = File.dirname(__FILE__).gsub("/" , "\\" )+ "\\" + fileName
save_dialog.ControlSend("Save As", "", "Edit1",path)
sleep 4
save_dialog.ControlClick("Save As", "Save &in", "&Save")
save_fileAlreadyExists = save_dialog.WinWait("Save As", " ", 5)
Solution 2 (Windows and Internet Explorer)
I use this method to save a file, when ever the dialog is called.
Remember to use click_no_wait and not click when clicking the button that initiates the download dialog
def save_file(filepath)
ai = WIN32OLE.new("AutoItX3.Control")
ai.WinWait("File Download", "", 5)
ai.ControlFocus("File Download", "", "&Save")
sleep 1
ai.ControlClick("File Download", "", "&Save", "left")
ai.WinWait("Save As", "", 5)
sleep 1
ai.ControlSend("Save As", "", "Edit1",filepath)
ai.ControlClick("Save As", "", "&Save", "left")
ai.WinWait("Download complete", "", 5)
ai.ControlClick("Download complete", "", "Close")
end
Usage:
ie.link(:id, "myLink").click_no_wait
save_file("C:\\file.txt")
Solution 3 (Windows and Internet Explorer)
Based on Solution 2. Modifications are addition of WinActivate and updating File Name to path+filename. Had to do this for IE8 on Win7 when saving several files to the same path (path was remembered so editing File Name to path and clicking Save had no effect).
def save_file(filepath)
ai = WIN32OLE.new("AutoItX3.Control")
ai.WinWait("File Download", "", 5)
ai.ControlFocus("File Download", "", "&Save")
sleep 1 #may not be necessary
ai.WinActivate("File Download")
sleep 1
ai.ControlClick("File Download", "", "&Save", "left")
ai.WinWait("Save As", "", 5)
sleep 1 #may not be necessary
fileName=ai.ControlGetText("Save As", "", "Edit1")
ai.ControlSend("Save As", "", "Edit1", "#{filepath}\\#{fileName}")
ai.ControlClick("Save As", "", "&Save", "left")
ai.WinWait("Download complete", "", 5)
ai.ControlClick("Download complete", "", "Close")
return fileName
end
Solution 4 (Windows and Firefox)
When you use do a file save in Firefox you dont get a save as dialog. So idea is to click on the download file button and then just press enter using autoit, as what I tried, it doesn't recognizes the window..
Here is an example script for the same:
require 'firewatir'
require 'watir'
require 'win32ole'
require 'rubygems'
ai = WIN32OLE.new("AutoItX3.Control")
ie=FireWatir::Firefox.new
#Go to AutoIt website
ie.goto("http://www.autoitscript.com/autoit3/downloads.shtml")
#Click Download AutoIt image
ie.image(:alt,'Download AutoIt').click
#Wait for the pop-up window with the specified title supplied to come
res=ai.WinWait("Opening autoit-v3-setup.exe","",30)
#Always try to Activate the pop-up window before using ControlClick fn()
res=ai.WinActivate("Opening autoit-v3-setup.exe")
ai.Send ("{ENTER}")