0

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

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.

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

Then I tried this 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();
        }
    }
}

Then I tried this didn't work

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++;
        }
}

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.

Nithin B
  • 601
  • 1
  • 9
  • 26

2 Answers2

1

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.

(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 Code

    <!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# code

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));
    }

}

Result:

elm.InnerHtml(DocumentCompleted):

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

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

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

MikeRays
  • 23
  • 6
0

I used OpenWebKitSharp to solved the problem that Html content rendered by js. If you can change the library, just go to this link to check the solution: Get final HTML content after javascript finished by Open Webkit Sharp

Community
  • 1
  • 1
nguyenhoai890
  • 1,189
  • 15
  • 20