2015-12-22 71 views
0

我的代碼現在我怎樣才能等到一個網站完成加載?

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Web.UI; 

namespace myweb 
{ 
    public partial class Form1 : Form 
    { 
     static Page page; 

     public Form1() 
     { 
      InitializeComponent(); 

      webBrowser1.ScriptErrorsSuppressed = true; 
      webBrowser1.Navigate("http://www.tapuz.co.il/forums/forumpage/393/%D7%98%D7%91%D7%A2_%D7%95%D7%9E%D7%96%D7%92_%D7%90%D7%95%D7%95%D7%99%D7%A8/%D7%9E%D7%96%D7%92_%D7%94%D7%90%D7%95%D7%95%D7%99%D7%A8"); 

     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 

     } 

     private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 
     { 
      string test "completed"; 
     } 
    } 
} 

的問題是,它的網頁它的自我完成加載之前獲得的DocumentCompleted事件幾次。我在字符串test =「completed」上使用了一個斷點;並在頁面完全加載之前停止了幾次。

我希望它會到那裏,並會使行字符串測試=「完成」;只有當網頁完全加載時才結束一次。

回答

0

試試這個:

public Form1() 
{ 
    InitializeComponent(); 

    webBrowser1.ScriptErrorsSuppressed = true; 
    webBrowser1.Navigate("http://www.tapuz.co.il/forums/forumpage/393/%D7%98%D7%91%D7%A2_%D7%95%D7%9E%D7%96%D7%92_%D7%90%D7%95%D7%95%D7%99%D7%A8/%D7%9E%D7%96%D7%92_%D7%94%D7%90%D7%95%D7%95%D7%99%D7%A8"); 
    while(webBrowser1.ReadyState != WebBrowserReadyState.Complete) 
    { 
     Application.DoEvents(); 
    } 
    MessageBox.Show("Site Loaded"); 
} 
3

每次一幀的負荷,在事件被觸發。

DocumentComplete可能會得到很多原因(框架,AJAX等)發射多次。同時,對於特定的文檔,window.onload事件將僅被觸發一次。所以,也許你可以在window.onload中進行處理。我只是試圖做到這一點。希望它有幫助。

private void Form1_Load(object sender, EventArgs e){ 
bool complete = false; 
this.webBrowser1.DocumentCompleted += delegate 
{ 
    if (complete) 
     return; 
    complete = true; 
    // DocumentCompleted is fired before window.onload and body.onload 
    this.webBrowser1.Document.Window.AttachEventHandler("onload", delegate 
    { 
     // Defer this to make sure all possible onload event handlers got fired 
     System.Threading.SynchronizationContext.Current.Post(delegate 
     { 
      // try webBrowser1.Document.GetElementById("id") here 
      MessageBox.Show("window.onload was fired, can access DOM!"); 
     }, null); 
    }); 
}; 

this.webBrowser1.Navigate("http://www.example.com");} 
相關問題