Link to home
Start Free TrialLog in
Avatar of krose777
krose777

asked on

Problem with axwebbrowser control - cant access innerhtml or any elements

I have an axwebbrowser control in a form.  I navigate to a page.  I am interested in getting the html from that page into a string variable.  For some reason, I cant use mypagedata = browser.Document.body.innerHTML like i would if it was a SHDocVw.InternetExplorer object.  In fact, any attempt to access similar elements always seem to result in the system crashing - not only that but I cant even get an error message most of the time (no source code available for current location).

Looking for a solution to get this data into a variable.  Also any documentation this object might be helpful.
Avatar of M_o_n_t_y
M_o_n_t_y

Hi KRose,

This worked for me:


Private Sub cmdAnalyze_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAnalyze.Click

        Dim oDoc As mshtml.HTMLDocument = AxWebBrowser1.Document
        MsgBox(oDoc.body.innerHTML)

End Sub

Let me know if that doesn't work for you...

                            -Monty
Oh yeah, I needed a reference to the Microsoft.mshtml .Net assembly (Under Project --> Add reference --> .Net tab).

                    -Monty
Avatar of krose777

ASKER

i get a "null reference exception".  If i inspect the object in a watch window and type in odoc.body.innerhtml, I get "referenced object Body has a value of nothing".  Why would this be the case?  

Other than that, i like the looks of this solution - if I can get it to work.  I like that the document methods/properties/etc are exposed, whereas whenever im accessing similar elements with an internet explorer com object its always late binding and the debugger doesnt check for spelling errors, etc.  Im assuming i can use this as a wrapper for SHDocVw.InternetExplorer too?  I will experiment.  
Sorry, you've got to load the document first and wait until the DocumentLoaded event fires on the browser. This code should be a little clearer: (create two buttons, one called cmdLoad and one called cmdAnalyze):


    Private Sub cmdLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLoad.Click
        cmdAnalyze.Enabled = False
        AxWebBrowser1.Navigate("https://www.experts-exchange.com")
    End Sub

    Private Sub AxWebBrowser1_DocumentComplete(ByVal sender As Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent) Handles AxWebBrowser1.DocumentComplete
        cmdAnalyze.Enabled = True
    End Sub

    Private Sub cmdAnalyze_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAnalyze.Click
        Dim oDoc As mshtml.HTMLDocument = AxWebBrowser1.Document
        MsgBox(oDoc.body.innerHTML)
    End Sub
cant get that bit of code to work.  The documentcomplete event never fires.  Wont:

                While Browser.Busy
                    Windows.Forms.Application.DoEvents()
                End While

or some other browser.property work ?
ASKER CERTIFIED SOLUTION
Avatar of M_o_n_t_y
M_o_n_t_y

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Monty -
I dont know why but i could never get yours to work properly - I was experimenting and this seems to work for some unknown reason:

 While Browser.Busy Or Not (Browser.ReadyState = SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE)
                    Windows.Forms.Application.DoEvents()
 End While

i was just checking for browser.busy, but that was unreliable for some reason.  Thank you for the help.