Solved

Problem with axwebbrowser control - cant access innerhtml or any elements

Posted on 2004-04-19
7
1,575 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help with LINQ and XML 10 36
Help with AsEnumerable(), LINQ 4 40
Creating a route in asp.net webforms 2 28
Error handling in asp.net site 5 28
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

809 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