【问题标题】:How to make WebBrowser wait till it loads fully?如何让 WebBrowser 等到它完全加载?
【发布时间】:2012-03-19 19:10:30
【问题描述】:

我有一个带有 Web 浏览器控件的 C# 表单。

我正在尝试循环访问不同的网站。

但是,我无法控制将 URL 地址加载到我的表单 Web 浏览器元素中。

这是我用于浏览 URL 地址的函数:

public String WebNavigateBrowser(String urlString, WebBrowser wb)
{
    string data = "";
    wb.Navigate(urlString);
    while (wb.ReadyState != WebBrowserReadyState.Complete)
    {
        Application.DoEvents();
    }
    data = wb.DocumentText;
    return data;
}

如何让我的循环等到它完全加载?

我的循环是这样的:

foreach (string urlAddresses in urls)
{
    WebNavigateBrowser(urlAddresses, webBrowser1);
    // I need to add a code to make webbrowser in Form to wait till it loads
}

【问题讨论】:

  • 请不要在标题前加上“c#”等。这就是标签的用途。
  • 将方法附加到DocumentCompleted 事件。
  • 好的!我这样做是因为当我尝试在 stackoverflow 上查找内容时它对我有帮助
  • 布莱恩,我该怎么做?你能给我一个示例代码行吗?

标签: c# winforms webbrowser-control


【解决方案1】:

将此添加到您的代码中:

webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);

填写这个函数

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
     //This line is so you only do the event once   
     if (e.Url != webBrowser1.Url) 
        return;


        //do you actual code        



    }

【讨论】:

  • Sam,我无法按照您的建议运行我的代码。它不工作
  • 请注意if 检查非常很重要。例如,DocumentedCompleted 也会为每个 iframe 触发。
  • 您确定将 webBrowser1 更改为 wb 吗?
  • 你能解释一下什么不起作用吗?我知道我发布的内容适用于我的代码。
  • 我需要循环运行该代码,因为 URL 有很多。该代码确实适用于第一个循环,但在第一个循环之后它不会运行。我有一个 HtmlElementCollection,我在其中收集访问过的网站的一些数据。在第一个循环中,我可以检索集合的数据,但是,在第二个循环中,它不会让我的代码等到网页加载,因此,我的集合为空,因此我的点击事件函数无法正常工作,所以循环中断。这是我的问题。如果有 NEXT click 可用,我的 click 事件处理。
【解决方案2】:

在对糟糕的 IE 功能感到愤怒一段时间后,我遇到了一些事情,这是判断页面加载完成的最准确方法。

永远不要使用 WebBrowserDocumentCompletedEventHandler 事件 使用 WebBrowserProgressChangedEventHandler 并进行一些修改,如下所示。

//“ie”是我们的网络浏览器对象

ie.ProgressChanged += new WebBrowserProgressChangedEventHandler(_ie);
private void _ie(object sender, WebBrowserProgressChangedEventArgs e)
{
  int max = (int)Math.Max(e.MaximumProgress, e.CurrentProgress);
  int min = (int)Math.Min(e.MaximumProgress, e.CurrentProgress);
  if (min.Equals(max))
  {
   //Run your code here when page is actually 100% complete
  }
}

解决这个问题的简单天才方法,我发现这个问题在谷歌上搜索“如何让网络浏览器休眠或暂停”

【讨论】:

  • 嗯...这对我的 javascript 完整问题没有帮助。 :(
  • 确认:这个解决方案不是绝对的。例如,这对我触发了 5 次,而 DocumentCompleted 仅触发了 2 次......所以,那个永恒的问题 "how to know when WB really finished loading document" 仍然悬而未决。
【解决方案3】:

根据MSDN(包含示例源),您可以为此使用DocumentCompleted 事件。可以在here 找到其他非常有用的信息和资源,这些信息和资源显示了如何区分事件调用。

【讨论】:

  • 实际上它不起作用。当我自己做时,它会为每个文档触发大约 4-5 次。
  • @Haedrian 它确实有效......它会为每个完成的文档调用,这意味着它可以为一个 URL 调用多次(例如,当它包含 IFRAME 或使用 AJAX 或使用重定向时)等等。)。按照第二个链接查看有关如何区分这些事件的建议...
【解决方案4】:

你所经历的事情发生在我身上。 readyStete.complete 在某些情况下不起作用。这里我在 document_completed 中使用 bool 来检查状态

 button1_click(){
    //go site1 
    wb.Navigate("site1.com");
    //wait for documentCompleted before  continue to  execute  any further 
    waitWebBrowserToComplete(wb); 

    // set some values in html page
    wb.Document.GetElementById("input1").SetAttribute("Value", "hello");
    //  then click submit. (submit does navigation)
    wb.Document.GetElementById("formid").InvokeMember("submit");
    // then wait for doc complete        
    waitWebBrowserToComplete(wb);


    var processedHtml = wb.Document.GetElementsByTagName("HTML")[0].OuterHtml;
    var rawHtml = wb.DocumentText;
}

// helpers
//instead of checking  readState . we get state from DocumentCompleted Event via bool value
bool webbrowserDocumentCompleted = false;
public static void waitWebBrowserToComplete(WebBrowser wb)
{
  while (!webbrowserDocumentCompleted )
      Application.DoEvents();
  webbrowserDocumentCompleted = false;
}

form_load(){
  wb.DocumentCompleted += (o, e) => {
     webbrowserDocumentCompleted = true;
  };
}

【讨论】:

    猜你喜欢
    • 2011-03-17
    • 2013-10-31
    • 1970-01-01
    • 2021-04-20
    • 1970-01-01
    • 1970-01-01
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多