Link to home
Start Free TrialLog in
Avatar of CDCOP
CDCOP

asked on

Load variable with html from webbrowser object

I need to load strVar1 with the data from a webpage using webbrowser object web1
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

1) Cast the WebBrowser's Document property to the corresponding mshtml.HtmlDocument class
2) Call document.getElementById or document.getElementByTagName to get at the HTML element.

Bob
Avatar of CDCOP
CDCOP

ASKER

Cqn you give me an example?
What does the HTML look like?

Bob
Avatar of CDCOP

ASKER

Lets say its a basic hello world line in the html.

I want to load that text, or the whole html from a webpage into a variable.
Avatar of CDCOP

ASKER

Is there anyway to get the response from a webpage? I was able to use:

Dim myHttpWebRequest2 As HttpWebRequest = CType(WebRequest.Create("http://www.cnn.com/asdasd"), HttpWebRequest)
        Dim myHttpWebResponse2 As HttpWebResponse = CType(myHttpWebRequest2.GetResponse(), HttpWebResponse)
        txt_1.Text = myHttpWebResponse2.StatusCode

But it only works if its a code 200. 404 will crash at runtime.
What .NET version?

Bob
Here is a 2003 example:

' Add a reference to MSHTML to your project.

Public Class formBrowser
  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.
  Private WithEvents browserGoogle As AxSHDocVw.AxWebBrowser
  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(formBrowser))
    Me.browserGoogle = New AxSHDocVw.AxWebBrowser
    CType(Me.browserGoogle, System.ComponentModel.ISupportInitialize).BeginInit()
    Me.SuspendLayout()
    '
    'browserGoogle
    '
    Me.browserGoogle.Dock = System.Windows.Forms.DockStyle.Fill
    Me.browserGoogle.Enabled = True
    Me.browserGoogle.Location = New System.Drawing.Point(0, 0)
    Me.browserGoogle.Size = New System.Drawing.Size(508, 423)
    Me.browserGoogle.TabIndex = 0
    '
    'formBrowser
    '
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(508, 423)
    Me.Controls.Add(Me.browserGoogle)
    Me.Name = "formBrowser"
    Me.Text = "Google Search"
    Me.WindowState = System.Windows.Forms.FormWindowState.Maximized
    CType(Me.browserGoogle, System.ComponentModel.ISupportInitialize).EndInit()
    Me.ResumeLayout(False)

  End Sub

#End Region

  Private m_document As mshtml.HTMLDocument

  Private m_url As String = "http://www.google.com/"

  Private Sub formBrowser_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated
    If Me.browserGoogle.Document Is Nothing Then
      Me.browserGoogle.Navigate2(m_url)
    End If
  End Sub

  Private Sub browserEE_DocumentComplete(ByVal sender As System.Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent) Handles browserGoogle.DocumentComplete

    If e.uRL = m_url Then
      m_document = Me.browserGoogle.Document

      Me.Google()
    End If

  End Sub

  Private Sub Google()

    Try

      For Each input As mshtml.HTMLInputElement In m_document.getElementsByTagName("input")

        If input.title = "Google Search" Then
          input.value = "define:  learned"
        ElseIf input.name = "btnG" Then
          input.click()

          Exit For
        End If

      Next input

    Catch ex As Exception

      MessageBox.Show(ex.ToString)

    End Try

  End Sub

End Class

Bob
Avatar of CDCOP

ASKER

What does your code do?

Can I load the whole webpage's html into a variable?
1) This is a form with a WebBrowser COM control on it, which navigates to Google, and does a lookup, and gets the results in the DocumentComplete event handler.  The Google method looks for certain <input> elements to set values and perform button clicks.

2) Yes, you can load the HTML into a variable, but what are you going to do with that text?

Bob
Avatar of CDCOP

ASKER

I would like to save the text into a text file.
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

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