使用 Watin 下载文件

发布于 2024-11-09 22:32:47 字数 725 浏览 5 评论 0原文

如何使用 watin 下载文件?我搜索了很多并尝试过,但我无法得到它。 我只想单击有下载的链接并保存它。我使用了我找到的例子,但没有成功。问题是我使用“WaitUntilFileDownloadDialogIsHandled(15)”,但 15 秒过去了并抛出异常: WatiN.Core.Exceptions.WatiNException :15 秒后未显示对话框。

这是代码:

FileDownloadHandler download = new FileDownloadHandler("C:/Development/Test/Downloads/" + "excel" + ".xls");
            using (new UseDialogOnce(browser.DialogWatcher, download))
            {
                browser.Button(Find.ById("id_of_the_button")).ClickNoWait();
                download.WaitUntilFileDownloadDialogIsHandled(15);
                download.WaitUntilDownloadCompleted(150);                
                browser.RemoveDialogHandler(download);
            }

请帮忙!

How can i download a file with watin? I searched a lot and tryied but i cant get it.
I only want to click on a link that have a download and save it. I used examples that i found but without success. The problem is that i use "WaitUntilFileDownloadDialogIsHandled(15)" but the 15 seconds pass and throw and exception: WatiN.Core.Exceptions.WatiNException : Has not shown dialog after 15 seconds.

This is the code:

FileDownloadHandler download = new FileDownloadHandler("C:/Development/Test/Downloads/" + "excel" + ".xls");
            using (new UseDialogOnce(browser.DialogWatcher, download))
            {
                browser.Button(Find.ById("id_of_the_button")).ClickNoWait();
                download.WaitUntilFileDownloadDialogIsHandled(15);
                download.WaitUntilDownloadCompleted(150);                
                browser.RemoveDialogHandler(download);
            }

Please, help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浊酒尽余欢 2024-11-16 22:32:48

我使用了以下代码片段,效果非常好。

FileDownloadHandler download = new FileDownloadHandler("C:\\Development\\Test\\Downloads\\" + "excel" + ".xls");
browser.AddDialogHandler(download);
browser.Button(Find.ById("id_of_the_button")).ClickNoWait();
download.WaitUntilFileDownloadDialogIsHandled(15);
download.WaitUntilDownloadCompleted(150);                
browser.RemoveDialogHandler(download);

我使用的代码片段中唯一的区别是我使用了带有转义字符的反斜杠,并且我在没有使用块的情况下添加和删除了 DialogHandler。

~阿希什·纳尔曼

I had used the following code snippet that worked absolutely fine

FileDownloadHandler download = new FileDownloadHandler("C:\\Development\\Test\\Downloads\\" + "excel" + ".xls");
browser.AddDialogHandler(download);
browser.Button(Find.ById("id_of_the_button")).ClickNoWait();
download.WaitUntilFileDownloadDialogIsHandled(15);
download.WaitUntilDownloadCompleted(150);                
browser.RemoveDialogHandler(download);

The only difference in the code snippet that I used was that I used a backslash with the escape character and that I added and removed the DialogHandler without an using block.

~Ashish Narmen

夏日落 2024-11-16 22:32:48

使用 WAIN 下载文档

 public FileDownloadHandler fileDownloadHandler;

 /*CLICK ON THE FILE LINK TO DOWNLOAD..IT WILL PROMPT FOR FILE DOWNLOAD DIALOG..TO HANDLE THAT DIALOG USE THE BELOW CODE*/

  fileDownloadHandler = new FileDownloadHandler(//THE PATH IN WHICH YOU DOWNLOAD DOCUMENTS//);
    try
    {
        using (new UseDialogOnce(ie.DialogWatcher, fileDownloadHandler))
        {                                               
          download();
        }            
    }
    catch (WatiN.Core.Exceptions.WatiNException ex)
    {
      download();
    }
    public void download()
    {
         try
        {
            fileDownloadHandler.WaitUntilDownloadCompleted(8);
        }
        catch (WatiN.Core.Exceptions.WatiNException ex)
        {
            download();
        }
    }

To Download Documents using WATIN

 public FileDownloadHandler fileDownloadHandler;

 /*CLICK ON THE FILE LINK TO DOWNLOAD..IT WILL PROMPT FOR FILE DOWNLOAD DIALOG..TO HANDLE THAT DIALOG USE THE BELOW CODE*/

  fileDownloadHandler = new FileDownloadHandler(//THE PATH IN WHICH YOU DOWNLOAD DOCUMENTS//);
    try
    {
        using (new UseDialogOnce(ie.DialogWatcher, fileDownloadHandler))
        {                                               
          download();
        }            
    }
    catch (WatiN.Core.Exceptions.WatiNException ex)
    {
      download();
    }
    public void download()
    {
         try
        {
            fileDownloadHandler.WaitUntilDownloadCompleted(8);
        }
        catch (WatiN.Core.Exceptions.WatiNException ex)
        {
            download();
        }
    }
眸中客 2024-11-16 22:32:47

我确实发现了一个困扰我的问题,

FileDownloadHandler handler = new FileDownloadHandler(@"c:\temp\file.csv");
browser.DialogWatcher.CloseUnhandledDialogs = false;
using (new UseDialogOnce(browser.DialogWatcher, handler))
{
    browser.Link(Find.ByText("July2011")).Click();
    handler.WaitUntilFileDownloadDialogIsHandled(15);
    handler.WaitUntilDownloadCompleted(240);
}

请注意包含“CloseUnhandledDialogs = false”行。我的保存对话框弹出窗口立即消失,我花了很长时间才意识到发生了什么。

I did discover one issue that plagued me

FileDownloadHandler handler = new FileDownloadHandler(@"c:\temp\file.csv");
browser.DialogWatcher.CloseUnhandledDialogs = false;
using (new UseDialogOnce(browser.DialogWatcher, handler))
{
    browser.Link(Find.ByText("July2011")).Click();
    handler.WaitUntilFileDownloadDialogIsHandled(15);
    handler.WaitUntilDownloadCompleted(240);
}

Note the inclusion of the "CloseUnhandledDialogs = false" line. My save dialog popup was disappearing immediately and it took me forever to realize what was going on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文