Solved

Problem with axwebbrowser control - cant access innerhtml or any elements

Posted on 2004-04-19
7
1,572 Views
Last Modified: 2008-03-06
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.
0
Comment
Question by:krose777
  • 4
  • 3
7 Comments
 
LVL 1

Expert Comment

by:M_o_n_t_y
ID: 10882444
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
0
 
LVL 1

Expert Comment

by:M_o_n_t_y
ID: 10882489
Oh yeah, I needed a reference to the Microsoft.mshtml .Net assembly (Under Project --> Add reference --> .Net tab).

                    -Monty
0
 
LVL 2

Author Comment

by:krose777
ID: 10884155
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.  
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 1

Expert Comment

by:M_o_n_t_y
ID: 10884258
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("http://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
0
 
LVL 2

Author Comment

by:krose777
ID: 10928758
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 ?
0
 
LVL 1

Accepted Solution

by:
M_o_n_t_y earned 500 total points
ID: 10929024
OK, create a new form called "test", open the code, clear it all out and replace it with this. (Make sure you have a reference to the Microsoft.mshtml .Net assembly). This definitely does work:

Public Class Test
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents AxWebBrowser1 As AxSHDocVw.AxWebBrowser
    Friend WithEvents cmdLoad As System.Windows.Forms.Button
    Friend WithEvents cmdAnalyze As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Test))
        Me.AxWebBrowser1 = New AxSHDocVw.AxWebBrowser
        Me.cmdLoad = New System.Windows.Forms.Button
        Me.cmdAnalyze = New System.Windows.Forms.Button
        CType(Me.AxWebBrowser1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'AxWebBrowser1
        '
        Me.AxWebBrowser1.Enabled = True
        Me.AxWebBrowser1.Location = New System.Drawing.Point(8, 8)
        Me.AxWebBrowser1.OcxState = CType(resources.GetObject("AxWebBrowser1.OcxState"), System.Windows.Forms.AxHost.State)
        Me.AxWebBrowser1.Size = New System.Drawing.Size(440, 328)
        Me.AxWebBrowser1.TabIndex = 5
        '
        'cmdLoad
        '
        Me.cmdLoad.Location = New System.Drawing.Point(264, 344)
        Me.cmdLoad.Name = "cmdLoad"
        Me.cmdLoad.Size = New System.Drawing.Size(88, 24)
        Me.cmdLoad.TabIndex = 6
        Me.cmdLoad.Text = "Load"
        '
        'cmdAnalyze
        '
        Me.cmdAnalyze.Location = New System.Drawing.Point(360, 344)
        Me.cmdAnalyze.Name = "cmdAnalyze"
        Me.cmdAnalyze.Size = New System.Drawing.Size(88, 24)
        Me.cmdAnalyze.TabIndex = 7
        Me.cmdAnalyze.Text = "Analyze"
        '
        'Test
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(464, 382)
        Me.Controls.Add(Me.cmdAnalyze)
        Me.Controls.Add(Me.cmdLoad)
        Me.Controls.Add(Me.AxWebBrowser1)
        Me.Name = "Test"
        Me.Text = "Test"
        CType(Me.AxWebBrowser1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub cmdLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLoad.Click
        cmdAnalyze.Enabled = False
        AxWebBrowser1.Navigate("http://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

End Class
0
 
LVL 2

Author Comment

by:krose777
ID: 10965638
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.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now