Tuesday, September 28, 2010

Using Watin to download files automatically

When using Watin, you can click on a link automatically or goto a download URL and save a file automatically using something similar to:


FileDownloadHandler fileDownloadHandler = new   FileDownloadHandler(featureCode2AnnouncementWavPath);browser.AddDialogHandler("file.zip");
browser.GoTo("http://www.mydomain.com/file.zip"); // OR: browser.Link(Find.ByID("download_button")).Click();
fileDownloadHandler.WaitUntilFileDownloadDialogIsHandled(15);
fileDownloadHandler.WaitUntilDownloadCompleted(20);

But IE will give you typically a notification bar warning of the type :
"IE has blocked site from downloading files to my computer" and then Watin will not download the file.


The only solution for IE8 that I found for this is to edit the registry and disable this prompting:


HKEY_LOCAL_MACHINE (or HKEY_CURRENT_USER)
     SOFTWARE
          Microsoft
               Internet Explorer
                    Main
                         FeatureControl
                              FEATURE_RESTRICT_FILEDOWNLOAD
                                   iexplore.exe = 0x00000000


currently, i manage to save a file using this method, but for some reason the code stops after clicking the save button and then times out with an exception:


 "Timeout while Internet Explorer state not complete"




I will update if i ever manage to solve this issue (it currently went down in priority)

1 comment:

  1. Please try this:
    1/ tool--> Internet options--> Security tab--> trusted sites--> Sites --> Add your site to the list
    2/ Turn off pop up blocker (I don't think step 2 even needed)
    I did not have to change the registry.

    ReplyDelete

Feel free to comment. No links/URLs allowed in comments.