使用Web浏览器控件等待Ajax加载

问题描述

| 我是.Net和C#的新手,在通过Web浏览器控件加载Ajax页面时需要帮助。 这是我当前正在使用的代码,导航至登录后,我正在搜索具有ajax调用的页面。
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);
        }
    }
方法waitTillLoad从这里 私有无效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++;
        }
    }
这工作正常,但我想知道确定ajax请求的确切加载时间。     

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)