我怎么能等到一个网站完成加载
本文关键字:网站 一个 加载 怎么能 | 更新日期: 2023-09-27 18:27:44
我的代码现在是
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"上使用了一个断点;在页面完全加载之前,它在那里停了几次。
我希望它能到达那里,并使字符串测试="完成";当网页被完全加载时,最终只有一次。
每次加载帧时,都会触发该事件。
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");}
试试这个:
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");
}