I am trying to upload a file using Watin v2.1 from C # 4.0 in IE9 and I have no luck. There are other questions asking a similar question, but none of the other answers load the file correctly in my situation, and I'm all tired of them.
Release 2.1 of Watin adds a new static method, ReturnDialogHandler.CreateInstance (), which should get the correct dialog handler for any version of IE. I cannot figure out how to implement this method.
The following code from Question here does not load 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 from Question here does not load the file in IE9. However, I was not 100% sure that CANCEL_LINK should be used. I tried to use the file name, file path for 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 also tried using SendKeys to manually save the file without using WatiN, and it doesn't seem consistent. It works a little differently every time and several times it doesnโt even download the file. Several times it does not rename the file, but downloads it. Here is 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 tried the suggestions listed by KMoraz, and also could not get any of them to work. I tried to go to the exact path to the file using ie.GoTo (filePathofFile) or find by ID and it will find the file but will not start the download. It seems I can find the file correctly, but it just won't load it. Can I do 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 not connected ... Did not show a dialog after 15 seconds. Using GoTo, the page does not load on the download page, but the file is in the notification panel and is ready to download. Any thoughts?
Additional information about the site: I need to log in to the https site using a username and password. After logging in, I get to the main page, where there are links "Download the file of the current day." I click on the text to download the current file. Once this link has been clicked, you will be taken to the download page. The file will appear in the notification panel for downloading in IE. There is also a link โIf the download window does not appear, clickโ Download file. โYou can click it directly to display the file in the notification panel to download the file.