简体   繁体   中英

C# WatiN Downloading Files IE9

I am trying to download a File using Watin v2.1 with C# 4.0 in IE9 and I am not having any luck. There are other questions asking the similar question but none of the other answers correctly download the file in my situation and I have tired them all.

The 2.1 release of Watin added a new static method ReturnDialogHandler.CreateInstance() that should get the correct dialoghandler for any version of IE. I can't figure out how to implement this method.

The following code take from Question Here does not download the file in IE9.

using(IE ie = new IE(someUrlToGoTo))
{
    FileDownloadHandler fileDownloadHandler = new FileDownloadHandler(fullFileName);
    ie.AddDialogHandler(fileDownloadHandler);

    ie.Link("startDownloadLinkId").ClickNoWait();

    fileDownloadHandler.WaitUntilFileDownloadDialogIsHandled(15);
    fileDownloadHandler.WaitUntilDownloadCompleted(200);
}

The following code take from Question Here does not download the file in IE9. However, I am was not 100% sure what should be used "CANCEL_LINK". I tried using the file name, file path to download, ect.

var cancel = browser.Link(Find.ByUrl(CANCEL_LINK));
var confirmDialog = new ConfirmDialogHandler();
using (new UseDialogOnce(browser.DialogWatcher, confirmDialog))
{
    cancel.ClickNoWait();
    confirmDialog.WaitUntilExists();
    confirmDialog.OKButton.Click();
    browser.WaitForComplete();
}

Update 1

I have also tried using SendKeys to Manually save the file without using WatiN and it doesn't seem to be consistent. It works slightly different every time and some times doesn't even download the file. A few times it doesn't rename the file, but does download it. Here the code:

System.Windows.Forms.SendKeys.SendWait("%N"); // Selects Notification Bar

System.Windows.Forms.SendKeys.SendWait("{TAB}");
System.Windows.Forms.SendKeys.SendWait("{DOWN 2}");  // Save As Option
System.Windows.Forms.SendKeys.SendWait("{ENTER}");
System.Windows.Forms.SendKeys.SendWait("DownloadedFile.txt");  // Enters File Name
System.Windows.Forms.SendKeys.SendWait("{ENTER}");

Update 2: 3/19

I have tried the suggestions listed by KMoraz and also couldn't get any of them to work. I have tried going to the exact file path using the ie.GoTo(filePathofFile), or the find by ID and it does find the file, but won't initiate the download. It appears I can find the file correctly, but it just won't download it. Could I be doing something out of order?

Updated Attempt:

string fullFileName = "https://mywebsite.com/files/area/download/ImportantFile.ZIP";

browser.GoTo(fullFileName);
FileDownloadHandler fileDownloadHandler = new FileDownloadHandler(fullFileName);
browser.AddDialogHandler(fileDownloadHandler);
fileDownloadHandler.WaitUntilFileDownloadDialogIsHandled(15);
fileDownloadHandler.WaitUntilDownloadCompleted(200);

I get the following exception: WatiN Exception was unhanded... Has not shown dialog after 15 seconds. By using the GoTo it doesn't take the page to the download page, but the file is in the notification bar and is ready for download. Any thoughts?

More information about the site: I have to sign in to https site using a login name and password. Once logged in I get to the main page where there is a links "Download Current Day's File". I click on the text to download the current file. Once this link has been clicked on it take you to a download page. The file will pop up in the notification bar for download in IE. There is a also a link "If Download Window does not appear, Please Click to Download File". It can be clicked directly to have the file appear in the notification bar to download the file.

The key is finding the right element and how to invoke it.

If this line doesn't work:

ie.Link("startDownloadLinkId").ClickNoWait();

It's possible that your control is of a different type:

ie.Button(Find.ById("startDownloadLinkId")).ClickNoWait();

or you can try a direct download:

ie.GoTo(fullFileName);

The point is you must grab the control type you need. You can query ie.Elements until you find it. Or use one of Find.By* methods if the id, name or type is known.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM