C # WatiN Download IE9 Files

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.

+4
source share
1 answer

The key finds the correct element and how to call it.

If this line does not work:

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

It is possible that your control has a different type:

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

or you can try direct download:

 ie.GoTo(fullFileName); 

The bottom line is that you have to grab the type of control you need. You can request ie.Elements until you find it. Or use one of the Find.By* methods if the name, name or type is known.

+1
source

Source: https://habr.com/ru/post/1401681/


All Articles