?
Solved

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

Posted on 2006-05-04
4
Medium Priority
?
660 Views
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.
Thanks
0
Comment
Question by:kayhustle
  • 4
4 Comments
 
LVL 96

Expert Comment

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

Imports System.Runtime.InteropServices

Public Enum CommandType
  Paste
End Enum

Public Class WebBrowserEx
  Inherits WebBrowser

  Private m_internalBrowser As SHDocVw.IWebBrowser2

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

  Protected Overrides Sub DetachInterfaces()
    MyBase.DetachInterfaces()
    Marshal.ReleaseComObject(m_internalBrowser)
  End Sub

  Public Sub ExecuteCommand(ByVal type As CommandType)

    Select Case type

      Case CommandType.Paste
        m_internalBrowser.ExecWB(SHDocVw.OLECMDID.OLECMDID_PASTE, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, Nothing, IntPtr.Zero)

    End Select

  End Sub

End Class

0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 16613802
Example form:

Public Class formBrowser

  Private m_document As HtmlDocument

  Private m_url As String = "http://ee-stuff.com/Expert/Upload/upload.php?Question=21839027"
  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
      Me.browser.Navigate(m_url)
    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("http://ee-stuff.com/accessLogin.php") Then
      Me.Login()
    Else
      Me.UploadFile()
    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"

    login.click()

  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

    file.focus()

    Clipboard.SetDataObject(m_fileName, copy:=True)

    Me.browser.ExecuteCommand(CommandType.Paste)

  End Sub

End Class
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16613808
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
            components.Dispose()
        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.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
    Me.browser = New Snippets.WebBrowserEx
    Me.SuspendLayout()
    '
    'browser
    '
    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
    '
    'formBrowser
    '
    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.Controls.Add(Me.browser)
    Me.Name = "formBrowser"
    Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
    Me.Text = "Browser"
    Me.ResumeLayout(False)

  End Sub
  Private WithEvents browser As WebBrowserEx
End Class

Bob
0
 
LVL 96

Expert Comment

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

Bob
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Screencast - Getting to Know the Pipeline
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month16 days, 8 hours left to enroll

862 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