【发布时间】:2012-03-15 19:30:49
【问题描述】:
我正在尝试在 IE9 中使用带有 C# 4.0 的 Watin v2.1 下载文件,但我没有任何运气。还有其他问题在问类似的问题,但在我的情况下,其他答案都没有正确下载文件,我已经厌倦了它们。
Watin 的 2.1 版本添加了一个新的静态方法 ReturnDialogHandler.CreateInstance(),它应该为任何版本的 IE 获取正确的对话框处理程序。我不知道如何实现这个方法。
以下代码取自Question Here 不会在 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);
}
以下代码取自Question Here 不会在 IE9 中下载文件。但是,我不是 100% 确定应该使用什么“CANCEL_LINK”。我尝试使用文件名、下载文件路径等。
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();
}
更新 1
我也尝试过使用 SendKeys 手动保存文件而不使用 WatiN,但似乎不一致。它每次的工作方式都略有不同,有时甚至不下载文件。有几次它不会重命名文件,但会下载它。代码如下:
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}");
更新 2:3/19
我已经尝试了 KMoraz 列出的建议,但也无法让它们中的任何一个起作用。我曾尝试使用 ie.GoTo(filePathofFile) 或按 ID 查找到确切的文件路径,它确实找到了文件,但不会启动下载。看来我可以正确找到该文件,但它不会下载它。我可以做一些不正常的事情吗?
更新尝试:
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);
我收到以下异常:WatiN Exception was unhanded... 15 秒后未显示对话框。通过使用 GoTo,它不会将页面带到下载页面,但文件在通知栏中并且可以下载。有什么想法吗?
有关该站点的更多信息:我必须使用登录名和密码登录 https 站点。登录后,我进入主页,其中有一个链接“下载当天的文件”。我单击文本以下载当前文件。单击此链接后,您将进入下载页面。该文件将在通知栏中弹出,以便在 IE 中下载。还有一个链接“如果没有出现下载窗口,请点击下载文件”。可以直接点击让文件出现在通知栏下载文件。
【问题讨论】:
-
试试这个话题的答案:stackoverflow.com/questions/7500339/…
-
我确实得到了上述信息来下载文件,但我想让WatiN处理下载。该文件已下载,但在程序完成运行之后可能会在同时运行多个不同任务时导致问题。感谢您的意见。
标签: c# visual-studio-2010 c#-4.0 internet-explorer-9 watin