开发者

Waiting for Ajax Load Using Web Browser Control

开发者 https://www.devze.com 2023-03-15 00:39 出处:网络
I am very new to .Net & C#, need help in loading Ajax page through Web Browser Control. Here is the code which currently I am using, after navigating to login I am in process of search page which

I am very new to .Net & C#, need help in loading Ajax page through Web Browser Control.

Here is the code which currently I am using, after navigating to login I am in process of search page which has ajax call.

private void searchNCPPageClick(string msg)
    {
        log.Debug("Processing Navigation to the NCP Search Project page...");
        try
        {
            HtmlElementCollection elems = this.webBrNcp.Document.GetElementsByTagName("html")[0].All;                
            foreach (HtmlElement elem in elems)
            {
                if (!(elem.DomElement.GetType().ToString().Equals("mshtml.HTMLAnchorElementClass")))
                {
                    continue;
                }

                if (elem.InnerHtml != null && elem.InnerHtml.Equals(IConstants.STR_SEARCH_PROJECT))
                {
                    elem.InvokeMember("click");

                    waitTillLoad(this.webBrNcp);
                    this.m_pageStatus = true;                        
                    this.m_page = this.webBrNcp.Document.GetElementsByTagName("html")[0].OuterHtml;
                    break;
                }
            }
            log.Debug("The Search Project Page navigated successfully.");
        }catch(Exception ex)
        {                
            log.Error("Error occurred while navigating Search Project page =" + ex.Message);
            string errMsg = String.Format("Error occurred while navigating Search Project page in {0} - {1}", "searchNCPPageClick", ex.Message);
            throw new ScreenScrapeException(errMsg, ex);
        }
    }

The method waitTillLoad is taken from this

private void waitTillLoad(WebBrowser webBrControl) {

        WebBrowserReadyState loadStatus;
        //wait till beginning of loading next page 
        int waittime = 100000;
        int counter = 0;
        while (true)
        {
            loadStatus = webBrControl.ReadyState;

            Application.DoEvents();

            if ((counter > waittime) || (loadStatus == WebBrowserReadyState.Uninitialized) || (loadStatus == WebBrowserReadyState.Loading) || (loadStatus == WebBrowserReadyState.Interactive))
            {
                break;
            }
            counter++;
        }       

        //wait till the page get loaded.
        counter = 0;
        while (true)
        {               
            loadStatus = webBrControl.ReadyState;
            Application.DoEvents();

            if (loadStatus == WebBrowserReadyState.Complete && webBrControl.IsBusy != true)
            {
                break;
            }
            counter++;
        }
    }

This works开发者_如何学C fine, but I would like to know to determine the exact load time of the ajax request.


Have you tried Watin? it is a free library that helps you monitor ajax while working with the webbrowser

or

you can have a look at this answer here on stack

0

精彩评论

暂无评论...
验证码 换一张
取 消