troubleshooting Question

webbrowser control: getting HTML document twice

Avatar of Irmawaty
Irmawaty asked on
Visual Basic.NET
2 Comments1 Solution587 ViewsLast Modified:
Hi everyone,

I have a sub that drives thru a website and it looks like this :

Private Sub AxWebBrowser1_DocumentComplete(ByVal sender As System.Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent) Handles AxWebBrowser1.DocumentComplete
        Select Case task
            Case 1
                Dim myDoc As HTMLDocument = New HTMLDocumentClass
                myDoc = CType(AxWebBrowser1.Document, HTMLDocument)

                Dim txtuser As HTMLInputElement
                txtuser = CType(myDoc.all.item("txtUser", 0), HTMLInputElement)
                txtuser.value = "xxx"

                Dim txtpass As HTMLInputElement
                txtpass = CType(myDoc.all.item("txtPass", 0), HTMLInputElement)
                txtpass.value = "xxx"

                Dim btnLogin As HTMLInputElement
                btnLogin = CType(myDoc.all.item("Login", 0), HTMLInputElement)
                btnLogin.click()

                task += 1
            Case 2 'going to another page
                AxWebBrowser1.Navigate2("https://xxx.xxx.com")
                task += 1

            Case 3
                Dim myDoc As HTMLDocument = New HTMLDocumentClass
                myDoc = CType(AxWebBrowser1.Document, HTMLDocument)

                try
                      Dim eventCat As HTMLSelectElement
                      events = CType(myDoc.all.item("Events", 0), HTMLSelectElement)
                      events.selectedIndex = 13
                      events.FireEvent("onchange", events)
                      task += 1
                catch ex as exception
                      console.WriteLine(ex.message.tostring)
                end try
         
            Case 4
                'continue filling out form

        End Select
    End Sub

The problem is when it gets to case 3, the first time it enters case 3, myDoc only holds the HTML <body> content of the page before <form>. It doesn't have any HTML for code inside <form>, so it will catch an exception: "Object reference not set to an instance of an object." on this line:
events = CType(myDoc.all.item("Events", 0), HTMLSelectElement).

Since task+=1 is not executed, it will go thry case 3 again, but this time myDoc contains the entire HTML code and successfully execute the code for case 3 and move on to case 4. The very same thing happens again in case 4.

I can still continue with what I am doing, but I am curious to know what happens to axWebbrowser1.document
Does anyone have any idea?
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 2 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros