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.inne rHTML 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.
Looking for a solution to get this data into a variable. Also any documentation this object might be helpful.
Oh yeah, I needed a reference to the Microsoft.mshtml .Net assembly (Under Project --> Add reference --> .Net tab).
-Monty
-Monty
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.
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_DocumentComp lete(ByVal sender As Object, ByVal e As AxSHDocVw.DWebBrowserEvent s2_Documen tCompleteE vent) Handles AxWebBrowser1.DocumentComp lete
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
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_DocumentComp
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
ASKER
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 ?
While Browser.Busy
Windows.Forms.Application.
End While
or some other browser.property work ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.READ YSTATE_COM PLETE)
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.
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.READ
Windows.Forms.Application.
End While
i was just checking for browser.busy, but that was unreliable for some reason. Thank you for the help.
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