WebBrowser DocumentComplete & ReadyState problems

Asked By Benjamin Cavallaro
05-Nov-09 09:09 PM
Earn up to 0 extra points for answering this tough question.
Im having problems here -

I can use the .navigate method on the webbrowser control to load a page. that works fine. but then when i access the .document property of the webbrowser control, look through the controls within the body, and then click a link on the page, there is no way to figure out when the page has finished loading - because i clicked a link rather than using the .navigate method of the control. so the problem then arises - how can i make my program sit around and wait for the next page to load?

Here are my solutions, none of which are working:
1) tried readystate or documentcomplete event - no luck, they just report everything is loaded bc not using navigate
2) trying to read from webbrowser1.document.body.innerhtml to see if there is anything on the page - this most often throws an error "Object reference not set to an instance of an object."
3) trying to read that, then catch the error and loop back around so the page can finish loading while im catching the error, so there will be an object to reference when i get back

none of this is working. i should mention that sometimes the error isnt thrown at all. sometimes everything loads up quick enough that the object exists to reference when the program is trying to do so. it clicks links on a few pages in a row, so each time i run it i get to a different step before the error is thrown (ie. this time it stopped after the first click, last time it stopped after the third click, etc.)

thoughts?

(code)

Imports System.Windows.Forms

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim jbl as HtmlElement
        Dim GotIt As Boolean
        GotIt = False

        'found first page, now click the first link
        For Each jbl In WebBrowser1.Document.GetElementsByTagName("A")
            If jbl.GetAttribute("href") = "javascript:send_event('SEARCH/SHOW_SEARCH');" Then
                jbl.InvokeMember("click")
                GotIt = True
                Exit For
            End If
        Next

        If GotIt = False Then 'otherwise couldnt find that link
            MsgBox("Please logout, login again and then stay at the homepage")
            GoTo cleaner
        End If
        GotIt = False 'reset this var

        While WebBrowser1.ReadyState <> 4
            Application.DoEvents()
        End While

        While Not WebBrowser1.DocumentTitle = "Test Page"
            Application.DoEvents()
        End While
        'if we make it thru, then page is loaded bc we found title

        Dim gr As String = WebBrowser1.Document.Body.InnerHtml
        Dim pl As Int32 = InStr(gr, "page 1 test")
        If pl <> 0 Then
            GoTo skipahead
        End If

I usually get the error at that point. 

  how i solved it

Benjamin Cavallaro replied to Benjamin Cavallaro
30-Dec-09 08:19 PM
i used documentcomplete event. i know i said it didnt work in my post, but something else was causing a problem.  that doesn't change the fact that the readystate property reports being finished prior to the documentcomplete event firing. the takeaway being: use documentcomplete, not readystate.
Create New Account