在webbrowser控件中调用ajax之后获取最新的html?

问题描述 投票:0回答:2

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

我有一个网页,在网页加载后调用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中的表内容因内部类而无法访问。

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

c# winforms webbrowser-control
2个回答
1
投票

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

这是以下代码,我希望这会引领您的解决方案。

(这里的想法是获取由AJAX生成的webBrowser1.Document.GetElementById("abc");的动态内容,并展示如何在onpropertychange上等待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(handlerAbc):06:32:36

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

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


0
投票

我使用OpenWebKitSharp来解决js呈现的Html内容的问题。如果您可以更改库,只需转到此链接以检查解决方案:Get final HTML content after javascript finished by Open Webkit Sharp

© www.soinside.com 2019 - 2024. All rights reserved.