I have a AXI camera that count the number of people entered the building and show the result on web page with video. The webpages update the count every time it detect a passing person. I believe the page has 2 iframes. Im trying to insert the value of the elementID ("count-in") which is the count result into a textbox after the webbrowserfully loaded. the code below gives me nothing. but if I inserted a button and click on it right after the page loaded, I got the count no problem! here is my code that doesnt work

I have tried this one

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim webBrowserX As New WebBrowser()

            ' Add an event handler that prints the document after it loads. 
            AddHandler webBrowserX.DocumentCompleted, New  _
                WebBrowserDocumentCompletedEventHandler(AddressOf showMe)

            ' Set the Url property to load the document.
            webBrowserX.Url = New Uri("http://xx.xx.xx.104/people-counter/lite/count.html")
        End Sub
    Private Sub showMe(ByVal sender As Object, _
            ByVal e As WebBrowserDocumentCompletedEventArgs)

            Dim webBrowserX As WebBrowser = CType(sender, WebBrowser)

            '  now that it is fully loaded.
            ' webBrowser get the value of the elementID ("count-in")
            Dim tableElem As HtmlElement = webBrowserX.Document.GetElementById("count-in")

            TextBox1.Text = TextBox1.Text & tableElem.InnerText


        End Sub

I have also tried this one,

Private Property pageready As Boolean = False

#Region "Page Loading Functions"
    Private Sub WaitForPageLoad()
        AddHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
        While Not pageready
            Application.DoEvents()
        End While

        pageready = False



    End Sub

    Private Sub PageWaiter(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
        If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then

            pageready = True

            RemoveHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
            ()
        End If

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.WebBrowser1.Navigate("http://xx.xx.xxx.104/people-counter/lite/count.html")

        Private Sub WebBrowser_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)_ 
        Handles WebBrowser1.DocumentCompleted

WaitForPageLoad()
Dim tableElem As HtmlElement = webBrowserX.Document.GetElementById("count-in")

        TextBox1.Text = TextBox1.Text & tableElem.InnerText

Recommended Answers

All 6 Replies

It looks like your doing this inside the form load event. Perhaps the textbox isn't fully loaded yet. Perhaps creating the textbox in runtime would ensure it get's fully loaded first.

Not really, the textbox loaded way before the webbrowser. Do you mean the webbrowser didn't have enough time to getelementID("COUNT-IN") value and inserted into textbox ?

sorry I weas little punchy whenm i worte that, the only thing I can see is put a break on then line where your adding text to the textbox and see if tableElem.InnerText is getting the right data. If isn't getting the right data, you might need a wait loop until it does. Also your second example appears to be missing some code.

Are you sure that tableElem has a value and is not nothing?

The DocumentCompleted event can fire many times when loading an url, I've found this technique very useful to make sure that the document is the one I want. Try this in the DecomentCompleted handler.

  If webBrowserX.Url = e.Url Then
     Dim tableElem As HtmlElement = webBrowserX.Document.GetElementById("count-in")
     If tableElem IsNot Nothing Then
        TextBox1.Text = TextBox1.Text & tableElem.InnerText

     Else
        Stop ' this is only for debugging

     End If
  End If

Thanks for your replay,
I'm sure there is a value because if insert a button with the get element id value code and click on it right after the page loaded, the value shows up inside the textbox.

it might still be a timing issue. You're firing the event after visually confirming the page is loaded. The software may not do that the way it sits. A simple conditional loop like TinTinMN suggested could be the solution. Either way putting a simple break on that line and examining your variables will tell the best way.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.