C# 如何让 WebBrowser 等到它完全加载?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9776359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 08:49:28  来源:igfitidea点击:

How to make WebBrowser wait till it loads fully?

c#winformswebbrowser-control

提问by Val Nolav

I have a C# form with a web browser control on it.

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

I am trying to visit different websites in a loop.

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

However, I can not control URL address to load into my form web browser element.

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

This is the function I am using for navigating through URL addresses:

这是我用于浏览 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;
}

How can I make my loop wait until it fully loads?

我怎样才能让我的循环等到它完全加载?

My loop is something like this:

我的循环是这样的:

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

采纳答案by SamFisher83

Add This to your code:

将此添加到您的代码中:

webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);

Fill in this function

填写这个功能

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        



    }

回答by Yahia

According to MSDN(contains sample source) you can use the DocumentCompletedevent for that. Additional very helpful information and source that shows how to differentiate between event invocations can be found here.

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

回答by user2887749

After some time of anger of the crappy IE functionality I've came across making something which is the most accurate way to judge page loaded complete.

在对蹩脚的 IE 功能感到愤怒一段时间后,我发现了一种判断页面加载完成的最准确方法。

Never use the WebBrowserDocumentCompletedEventHandler event use WebBrowserProgressChangedEventHandler with some modifections seen below.

永远不要使用 WebBrowserDocumentCompletedEventHandler 事件使用 WebBrowserProgressChangedEventHandler 和下面看到的一些修改。

//"ie" is our web browser object

//"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
  }
}

Simple genius method of going about this, I found this question googling "How to sleep web browser or put to pause"

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

回答by bh_earth0

what you experiencend happened to me . readyStete.complete doesnt work in some cases. here i used bool in document_completed to check state

你所经历的发生在我身上。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;
  };
}