在web浏览器控件中调用ajax后获取最新的html

本文关键字:获取 最新 html ajax 调用 web 浏览器 控件 | 更新日期: 2023-09-27 18:08:31

有很多这样的问题,我无法找到解决我的问题的方法。

我有一个网页,网页加载后,Ajax被调用,它将加载一个表的数据可能需要2秒。

我想要那个表里面的数据

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

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,由于内部类无法访问。

还有其他方法可以解决我的问题吗

在web浏览器控件中调用ajax后获取最新的html

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

下面是代码,我希望这能引导你找到解决方案。

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

    <!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 (documentcomplete事件):

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

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

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

我使用OpenWebKitSharp来解决Html内容被js渲染的问题。如果您可以更改库,请到此链接查看解决方案:使用Open Webkit Sharp

获取javascript完成后的最终HTML内容。