1,105,534 Community Members

firing an event after webbrowser completed with iframe

Member Avatar
yous
Newbie Poster
6 posts since Nov 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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
Member Avatar
tinstaafl
Postaholic
2,012 posts since Jun 2010
Reputation Points: 559 [?]
Q&As Helped to Solve: 402 [?]
Skill Endorsements: 35 [?]
 
0
 

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.

Member Avatar
yous
Newbie Poster
6 posts since Nov 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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 ?

Member Avatar
tinstaafl
Postaholic
2,012 posts since Jun 2010
Reputation Points: 559 [?]
Q&As Helped to Solve: 402 [?]
Skill Endorsements: 35 [?]
 
0
 

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.

Member Avatar
TnTinMN
Practically a Master Poster
640 posts since Jun 2012
Reputation Points: 418 [?]
Q&As Helped to Solve: 152 [?]
Skill Endorsements: 16 [?]
 
0
 

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
Member Avatar
yous
Newbie Poster
6 posts since Nov 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
tinstaafl
Postaholic
2,012 posts since Jun 2010
Reputation Points: 559 [?]
Q&As Helped to Solve: 402 [?]
Skill Endorsements: 35 [?]
 
0
 

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.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: