I have created an windows application which loads a web page. I need to check the page load time of the web page. I cannot use a timer and note the time at document completed event because the page is redirecting to some other page and it has lots of iframes as well so the document completed event fires many times ( around 10). So how to know that the page has completed loading? I can count the number of document completed events on that page and then stop the timer when it is called for the last time. But I want a generic method. Please guide me regarding this.
Thanks
如下是一种解决方案:
Hi kevin Dark:
I think I know your questions now. You mean when you load a website page with using webBrowser, the page redirects may times and you just want to know when the last page is loaded complete.
So, we can use DocumentCompleted event to tell us as below:
private void webBrowser1_DocumentCompleted( object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (webBrowser1.ReadyState == WebBrowserReadyState .Complete)
{
this .textBox1.Text = webBrowser1.Document.Url.ToString(); //each redirect url may be shown
if (webBrowser1.Document.Url.ToString() == “the final url string” )
{
MessageBox .Show( “Load Complete!” );
}
}
}
If you have any questions, please feel free to tell us.
Best Regards
I use the url< http://www.live.com> to load by webbrowser and login in, when i sign out the live.com, it will redirect the “http://www.bing.com/” and I get the last redirection.
但是有时候会有问题,比如Ajax请求并不会触发完成事件.
此时,可用另一种方案,确定一个变化的Element,为该元素增加事件
// Get the target
(e.g)
HtmlElement target = _webBrowser.Document.GetElementById(“somedivthatwillbepopulatedbytheajaxrequest”);
if (target != null)
{
target.AttachEventHandler(“onpropertychange”, new EventHandler(handler));
}
The event will fire whenever the element is (re)populated with HTML so all you do is implement the handler so you can check the InnerHTML or InnerText or whatever it is you need to do, and do it to your hearts content Wink
(e.g)
private void handler(Object sender, EventArgs e)
{
HtmlElement div = _webBrowser.Document.GetElementById(“somedivthatwillbepopulatedbytheajaxrequest”);
if (div == null) return;
String x = div.InnerHtml; // etc
if (!x.Equals(“Loading…”, StringComparison.InvariantCultureIgnoreCase))
{
// Now the element has been populated, do something
}
}
CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明Webbrowser中统计加载指定页面所需时间,判断Ajax请求完成!