C # WebBrowser accurately determine the final loading of the Web page

Source: Internet
Author: User

==

Recently wrote a software called Webautoscript, for the purpose of the automatic processing of web pages, that is, all your actions on the Web page can be recorded in a script, and then you can play back the operation process. I mean, any process.

The program is written in C #, which has encountered a problem, for the Web page, I am using C # built-in WebBrowser control control, but this control has a problem, is not accurate to determine when the final loading of the page, if this can not be judged, I can not judge when to perform the next action, Because the Web page is not open, it is not possible to click the button that has not yet appeared ... Google at home and abroad, no one has done, unless the control, although there are other controls can, such as Axwebbrowser, but with another control, there will be another problem, so finally decided to use the built-in WebBrowser, but how to solve the problem ...

Why this control can not accurately determine when the page is loaded, it is because the current page generally has more than one frame, a frame equivalent to another page, in constant loading, the message constantly triggered, so it is difficult to determine the trigger to complete the page ...

On the internet is generally said according to browser_documentcompleted,browser_navigated,browser_navigating these three messages to judge, with what counter, and Judge IsBusy, StatusText, etc... More or less for some of the pages are feasible, for other pages but there is another accident, so in general, can not be universal accurate ...

In the end, a Web page is eventually loaded, no matter how many frames it has, and it is finished only once, and is a message that can be provided to the program to perform the next action.

How to solve, in fact, WebBrowser work mechanism is probably like this, on the URL to play www.163.com, this time, WebBrowser will start loading www.163.com This page, triggering a browser_navigating, The readystate is set to a non-complete,isbusy true,statustext as the "Requested URL content", which, when finished, triggers a browser_documentcompleted, and The readystate is set to Complete,isbusy false,statustext to "done", and if there is usually a frame, such as WWW.163.COM/KK.JSP#FSD or something, Webbrower will trigger the browser_navigating,readystate to be complete,isbusy to True,statustext as "requested URL content" and then to load that frame, Triggers a browser_documentcompleted, while the readystate is set to Complete,isbusy false,statustext to "done", the rest of the frame, and the remainder of the analogy.

The above process has a feature, from the main document to the frame of the process, readystate set to Complete,isbusy False,statustext set to "done", this time is very short, browser_ DocumentCompleted triggered the past, if there is a frame, will soon put the state into a non-completed, so according to this feature ... We can find a way to judge when it's finally done ...

My method is: When WebBrowser started loading www.163.com This page, when triggered browser_navigating, I opened a timer, where monitoring, interval for 200ms, If mainbrowser readystate = = Webbrowserreadystate.complete && IsBusy = False, it proves that there are two cases, WebBrowser has finally loaded, Or just load the end of one, will soon load another frame, regardless, first record this time Documenttext,documenttitle,documenttitle,url, wait until the next 200ms, Timer again judge now webbrowser readystate = = Webbrowserreadystate.complete && IsBusy = False, if not set, then the status of the record is cleared, Those documenttext, now certainly not to the final completion of the state, but, if established, the current state of WebBrowser and the previous timer record of the state compared, if not equal, it can only indicate that has not reached the final state, if the same, set a register, Plus 1, so if this counter can finally reach 5 times, it means that WebBrowser finally finished loading ... Simply put, is to add a timer to monitor WebBrowser several important state values, if in 5*200ms a second, its state has not changed, it proves that it has finally loaded ...

This way, hundred test Lark, did not encounter the situation of miscalculation, hehe, strong bar, Ms no perfect place, we find a way to Bai ...

# C #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.