在网页浏览器控件中调用ajax后获取最新的html? [英] Get the latest html after ajax call in webbrowser control?

查看:47
本文介绍了在网页浏览器控件中调用ajax后获取最新的html?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此类问题太多了,但我无法找到解决问题的方法.

There are lot of this kind of questions and I was not able to find a solution for my problem.

我有一个网页,在网页加载后 Ajax 被调用,它会加载一个包含数据的表格,可能需要 2 秒.

I have a webpage and after the webpage loads Ajax is called and it will load a table with data may be it takes 2 seconds.

我想要那个表中的数据.

I want the data inside that table.

当我尝试使用文档文本访问表格时 它没有表格 HTML.它仍然具有在 Ajax 调用之前加载的初始 HTML.

When I try to access the table using document text It does not have the table HTML. It still have the initial HTML that has loaded before Ajax call.

webBrowser1.Update(); //Didn't work

然后我试了一下没用

private void Timer_Tick(object sender, EventArgs e) //Interval of 5000
{
    if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)
    {
        HtmlElement element = webBrowser1.Document.GetElementById("tableType3");
        if (element != null)
        {
            String webbrowsercontent = element.InnerHtml;
            timer.Stop();
        }
    }
}

然后我试了一下没用

private void WaitTillPageLoadsCompletly(WebBrowser webBrControl)
{
        WebBrowserReadyState loadStatus;
        int waittime = 20000;
        int counter = 0;
        while (true)
        {
            loadStatus = webBrControl.ReadyState;
            Application.DoEvents();
            if ((counter > waittime) || (loadStatus == WebBrowserReadyState.Uninitialized) || (loadStatus == WebBrowserReadyState.Loading) || (loadStatus == WebBrowserReadyState.Interactive))
            {
                break;
            }
            counter++;
        }
        counter = 0;
        while (true)
        {
            loadStatus = webBrControl.ReadyState;
            Application.DoEvents();
            if (loadStatus == WebBrowserReadyState.Complete && webBrControl.IsBusy != true)
            {
                break;
            }
            counter++;
        }
}

调试时看到WebBrowser1.Document.NativeHtmlDocument2中的表格内容由于内部类无法访问.

In debugging I saw the table contents in WebBrowser1.Document.NativeHtmlDocument2 which cant be accessed because of internal class.

有没有其他方法可以解决我的问题.

Is there any other way to solve my problem.

推荐答案

您是否尝试过监听 Ajax onpropertychange 事件?我最近访问了一个网站,该网站教授如何在 webBrowser1_DocumentCompleted 中处理 Ajax 组件 onpropertychange 事件.

Have you tried listening to the Ajax onpropertychange event? I've recently visited a website that teaches how to handle a Ajax component onpropertychange event in webBrowser1_DocumentCompleted.

这是以下代码,我希望这可以为您提供解决方案.

Here's the following code, I hope this leads the way to your solution.

(这里的想法是获取webBrowser1.Document.GetElementById("abc");的AJAX生成的动态内容,并展示如何等待webBrowser1_DocumentCompleted)

(The idea here is to get webBrowser1.Document.GetElementById("abc");'s dynamic content generated by AJAX, and show how you can wait on the onpropertychange event in webBrowser1_DocumentCompleted)

HTML 代码

    <!DOCTYPE HTML>
<html lang="en-US">
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <script>
            $.ajaxSetup({
                cache: false
            });
            var aa = function() {
                $.get("ajax.php", function(data) {
                    $("#abc").html(data);
                });
            };

            $(function() {
                aa();
                setInterval(aa, 2000);
            });
        </script>
    </head>
    <body>
        <div id="abc"></div>
    </body>
</html>

ajax.php

    <?php
echo date("H:i:s");

C# 代码

private void button1_Click(object sender, EventArgs e)
{
    webBrowser1.Navigate("http://127.0.0.1/test.html");
}

private void handlerAbc(Object sender, EventArgs e)
{
    HtmlElement elm = webBrowser1.Document.GetElementById("abc");
    if (elm == null) return;
    Console.WriteLine("elm.InnerHtml(handlerAbc):" + elm.InnerHtml);
}

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    /* Get the original HTML (method 1)*/
    System.IO.StreamReader getReader = new System.IO.StreamReader(webBrowser1.DocumentStream, System.Text.Encoding.Default);
    string htmlA = getReader.ReadToEnd(); // htmlA can only extract original HTML

    /* Get the original HTML (method 2)*/
    string htmlB = webBrowser1.DocumentText; // htmlB can only extract original HTML

    /* You can use the following method to extract the 'onChanged' AJAX content*/
    HtmlElement elm = webBrowser1.Document.GetElementById("abc"); // Get "abc" element by ID
    Console.WriteLine("elm.InnerHtml(DocumentCompleted):" + elm.InnerHtml);
    if (elm != null)
    {
        // Listen on 'abc' onpropertychange event
        elm.AttachEventHandler("onpropertychange", new EventHandler(handlerAbc));
    }

}

结果:

elm.InnerHtml(DocumentCompleted):

elm.InnerHtml(DocumentCompleted):

elm.InnerHtml(handlerAbc):06:32:36

elm.InnerHtml(handlerAbc):06:32:36

elm.InnerHtml(handlerAbc):06:32:38

elm.InnerHtml(handlerAbc):06:32:38

elm.InnerHtml(handlerAbc):06:32:40

elm.InnerHtml(handlerAbc):06:32:40

这篇关于在网页浏览器控件中调用ajax后获取最新的html?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆