Set text or Click buttons using WebBrowser Control in .net 2.0

Posted on 2006-05-04
Last Modified: 2008-03-04
Can anybody show me how I would grab Button elements, and click them or Input elements and set their text using the new WebBrowser control in .net 2.0?  I've done this with the COM version, but can't find how to do it in the .net 2.0 version.
Question by:kayhustle
    LVL 96

    Expert Comment

    by:Bob Learned
    VB.NET 2005 extended WebBrowser control version:

    Imports System.Runtime.InteropServices

    Public Enum CommandType
    End Enum

    Public Class WebBrowserEx
      Inherits WebBrowser

      Private m_internalBrowser As SHDocVw.IWebBrowser2

      Protected Overrides Sub AttachInterfaces(ByVal nativeActiveXObject As Object)
      End Sub

      Protected Overrides Sub DetachInterfaces()
      End Sub

      Public Sub ExecuteCommand(ByVal type As CommandType)

        Select Case type

          Case CommandType.Paste

        End Select

      End Sub

    End Class

    LVL 96

    Accepted Solution

    Example form:

    Public Class formBrowser

      Private m_document As HtmlDocument

      Private m_url As String = ""
      Private m_fileName As String = "E:\Visual Studio 2005 Projects\Snippets\Snippets\formBrowser.vb"

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

      Private Sub browser_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles browser.DocumentCompleted

        m_document = Me.browser.Document
        m_url = e.Url.ToString()

        If m_url.StartsWith("") Then
        End If

      End Sub

      Private Sub Login()

        Dim user As mshtml.HTMLInputElement = Nothing
        Dim password As mshtml.HTMLInputElement = Nothing
        Dim login As mshtml.HTMLInputElement = Nothing

        For Each element As HtmlElement In m_document.GetElementsByTagName("input")

          If element.Name = "Username" Then
            user = element.DomElement
          ElseIf element.Name = "Password" Then
            password = element.DomElement
          ElseIf CType(element.DomElement, mshtml.HTMLInputElement).value = "Login" Then
            login = element.DomElement
          End If

        Next element

        user.value = "user id goes here"
        password.value = "password goes here"

      End Sub

      Private Sub UploadFile()

        Dim file As mshtml.HTMLInputElement = Nothing

        For Each element As HtmlElement In m_document.GetElementsByTagName("input")

          If element.Name = "eeFile" Then
            file = element.DomElement
            Exit For
          End If

        Next element


        Clipboard.SetDataObject(m_fileName, copy:=True)


      End Sub

    End Class
    LVL 96

    Expert Comment

    by:Bob Learned
    Form .designer file:

    <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
    Partial Class formBrowser
        Inherits System.Windows.Forms.Form

        'Form overrides dispose to clean up the component list.
        <System.Diagnostics.DebuggerNonUserCode()> _
        Protected Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing AndAlso components IsNot Nothing Then
            End If
        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.
        <System.Diagnostics.DebuggerStepThrough()> _
        Private Sub InitializeComponent()
        Me.browser = New Snippets.WebBrowserEx
        Me.browser.Dock = System.Windows.Forms.DockStyle.Fill
        Me.browser.Location = New System.Drawing.Point(0, 0)
        Me.browser.MinimumSize = New System.Drawing.Size(20, 20)
        Me.browser.Name = "browser"
        Me.browser.Size = New System.Drawing.Size(567, 566)
        Me.browser.TabIndex = 0
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(567, 566)
        Me.Name = "formBrowser"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "Browser"

      End Sub
      Private WithEvents browser As WebBrowserEx
    End Class

    LVL 96

    Expert Comment

    by:Bob Learned
    Your project needs a reference to ShDocVw from \Windows\System32, and MSHTML from the COM tab.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    779 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

    14 Experts available now in Live!

    Get 1:1 Help Now