C # WebBrowser accurately determines that the web page is finally loaded

Source: Internet
Author: User

Recently I wrote a software called WebAutoScript for Automatic Webpage operations. That is to say, all your operations on the webpage can be recorded in a script, then, you can play back the operation .. I mean any process.

The program was written in C #, and encountered a problem. For webpages, I used the built-in WebBrowser control in C #, but this control has a problem, I cannot accurately determine when the webpage is finally loaded. If this cannot be determined, I cannot determine when to perform the next action because the webpage has not been opened, it is impossible to click a button that has not yet appeared... No one has done this for google at home and abroad. Unless this control is not used, although there are other controls, such as AxWebBrowser, there will be another problem with other controls, so we finally decided to use the built-in WebBrowser, but how can we solve this problem...

Why is this control unable to accurately determine when the webpage is loaded? It is because the current webpage usually has multiple frames. One frame is equivalent to another webpage, which is continuously loaded and messages are continuously triggered, so it is difficult to determine which web page is triggered...

The Internet is generally based on the Browser_DocumentCompleted, Browser_Navigated, and Browser_Navigating messages. What counters are used, as well as IsBusy and StatusText .. The above is more or less feasible for some web pages, but there are other accidents for some other web pages, so in general, it is not universally accurate...

In general, a web page is finally loaded, no matter how many frames are in it, only once is finally completed. It is also a message that can be provided to the program to perform the next operation.

How can we solve this problem? In fact, the working mechanism of webbrowser is probably like this. When www.163.com is added to the URL, webbrowser starts to load the webpage www.163.com and triggers a browser_navigating. readystate is set to non-complete, isbusy is set to true, and statustext is set to "request URL content". After this document is complete, a browser_documentcompleted is triggered. At the same time, readystate is set to complete, and isbusy is set to false, statustext is set to "complete". If there is a frame, such as the requested URL content, you can load the frame, After the completion, a browser_documentcompleted is triggered. At the same time, the readystate is set to complete, the isbusy is set to false, the statustext is set to "complete", and the remaining frame is set to the remainder, and so on.

The above process has a feature. From the main document to the frame process, readystate is set to complete, isbusy is set to false, and statustext is set to "complete". This time is very short, the browser_documentcompleted trigger is passed. If there is a frame, the status will soon become incomplete, so according to this feature... We can find a way to determine when the final process is completed...

My method is: When WebBrowser starts loading the webpage www.163.com and triggers Browser_Navigating, I open a timer and monitor it there, Interval is 200 ms, if the ReadyState of MainBrowser is = WebBrowserReadyState. if Complete & IsBusy = false, it indicates that the WebBrowser has been finally loaded, or the other frame will be loaded soon, first, record the DocumentText, DocumentTitle, DocumentTitle, Url at this time. Wait until the next 200 ms, timer then judges the current WebBrowser ReadyState = WebBrowserReadyState. complete & IsBusy = false. If not, clear the record status. OcumentText is not in the final completion state yet. However, if it is true, compare the current WebBrowser status with the status of the last timer record. If it is not equal, it can only indicate that it has not reached the final state. If it is the same, set a counter and Add 1. In this way, if the counter can eventually reach 5 times, it indicates that WebBrowser is finally loaded... Simply put, add another timer to monitor several important status values of WebBrowser. If the status has not changed within 5 * ms, it proves that, it has finally been loaded...

In this way, we have never encountered any misjudgment. Ha, strong, MS is not perfect. Let's find a solution by ourselves...

 

Related Article

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.