November 21, 2008   Login   |   Register  
  Search  
  |  
  Tips & Tricks | Developers Corner | Web Form Submitter

Submit Web Forms in VB.NET

Minimize

Skill Level Required: Requirements:
Medium Visual Studio
Intermediate VB.NET knowledge
Intermediate HTML knowledge
Intermediate DOM knowledge
Download Source Code

Intro

Adding a web browser to an application is pretty simple - just drag and drop the AxWebBrowser control from the Visual Studio toolbox. However, it's also pretty useless unless you can do something with it. The Renegade Web Form Submitter is a sample application to show you how you can manipulate HTML documents in a web browser and submit web forms.

The AxWebBrowser (shdocvw.dll) is basically just Internet Explorer functionality.

Add References

Your project needs to reference the AxWebBrowser (shdocvw.dll) and MSHTML (MSHTML.tlb).

Drag and Drop a web browser control from your Visual Studio toolbox onto your Windows form design surface. 

Next, you need to add a reference to the MSHTML type library. In the Solution Explorer, expand References, right-click on References, and choose Add Reference...

Click the COM tab, and browser to the WINNT/System32 directory, and add the reference to the MSHTML dll or tlb. Next, add "Imports mshtml" to the top of your form.

Your ready to start now.

Add Code

Add the following line at the top of your Windows form:

        Private objDoc As New mshtml.HTMLDocument ' This is going to hold the document in the browser for us and allow us to access it and change it

Add text boxes and buttons as shown by the screen shot of the application above. Double click the form and add some code to the Load event:

        Private Sub frmWebSubmit_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim strNavMessage As String
            strNavMessage = "Browser" ' This will be displayed in the browser window
            ' Once the program loads, navigate to a 'null' page and display a short text message. This avoid the ugliness of an browser window with nothing in it at all.
            wbrBrowser.Navigate("about:" & strNavMessage)
        End Sub

That will display an essentially empty page for you when the application loads.

Double click the Go to URL button and add:

        Private Sub btnGoUrl_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGoUrl.Click
            ' Navigate to the URL
            wbrBrowser.Navigate(txtUrl.Text)

            ' Let the user know that work is in progress...
            stbStatus.Text = "Loading URL..."

        End Sub

That will change the status bar for you. I've left out other places to do this, but you can easily add it in wherever you want.

Next, add some code to the Fill Input button. It is explained as you read along through the code.


        Private Sub btnFillInput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFillInput.Click
            ' *** IMPORTANT *** This is where we set our MSHTML document object equal to our axWebBrowser (shdocvw.dll) - This will allow us to access our browser object, i.e. We now have access to the DOM
            objDoc = wbrBrowser.Document

            ' text input for Google is "q" - submit button for Google is btnG - I'm feeling lucky submit button is "btnI"

            ' Create a collection of all the "input" elements in the page.
            Dim wbrAll As mshtml.IHTMLElementCollection = objDoc.getElementsByTagName("input")
            ' Create an object that will be a single instance of an input element
            Dim wbrElm As mshtml.IHTMLElement

            ' Create a few variable to hold values from the input element
            Dim strName As String
            Dim strId As String
            Dim strvalue As String
            Dim strType As String

            Try
                ' Iterate through the collection of html elements, i.e. our input collection
                For Each wbrElm In wbrAll

                    ' Assign the inner html values of the input to our variables
                    strName = wbrElm.getAttribute("name")
                    strId = wbrElm.id
                    strvalue = wbrElm.innerText
                    strType = wbrElm.getAttribute("type")

                    ' We are only interested in filling text boxes, and only interested in a specific  one, i.e. "q"
                    If strType.ToString.ToLower = "text" And strName.ToString.ToLower = txtInputName.Text.ToLower Then
                        ' Set the "value" with the setAttribute method.
                        wbrElm.setAttribute("value", txtInputValue.Text)
                        ' Scroll the page so that we can see the input is filled.
                        wbrElm.scrollIntoView()

                    End If

                Next
            Catch ex As Exception
                ' If there is an error, it is for the reason stated below - you can change the code to be more robust or to handle different pages.
                MessageBox.Show("It is likely that your input does not exist or has no name attribute. Check the HTML source.", "No name att. or no input available", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End Try

        End Sub

That was pretty simple, so we only have to submit the form now:

        Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
            ' *** IMPORTANT *** This is where we set our MSHTML document object equal to our axWebBrowser (shdocvw.dll) - This will allow us to access our browser object, i.e. We now have access to the DOM
            objDoc = wbrBrowser.Document

            ' text input for Google is "q" - submit button for Google is btnG - I'm feeling lucky submit button is "btnI"

            ' Create a collection of all the "input" elements in the page.
            Dim wbrAll As mshtml.IHTMLElementCollection = objDoc.getElementsByTagName("input")
            ' Create an object that will be a single instance of an input element
            Dim wbrElm As mshtml.IHTMLElement

            ' Create a few variables to hold values from the input element - you can create more
            Dim strName As String
            Dim strId As String
            Dim strValue As String
            Dim strType As String

            Try
                ' Iterate through the collection of html elements, i.e. our input collection
                For Each wbrElm In wbrAll

                    ' Assign the inner html values of the input to our variables
                    strName = wbrElm.getAttribute("name")
                    strId = wbrElm.id
                    strValue = wbrElm.innerText
                    strType = wbrElm.getAttribute("type")

                    ' We are only interested in submitting the form right now, so, this time around we'll do the normal submit button
                    If strType.ToString.ToLower = "submit" And strName.ToString.ToLower = txtSubmitName.Text.ToLower Then

                        ' Scroll the page so that we can see button that we are clicking - totally not necessary - but makes the user thing something is happening - kind of like lights on early mainframes
                        wbrElm.scrollIntoView()

                        ' Create an object to access the DOM and get to the submit button
                        Dim btnSearch As HTMLInputElement
                        ' Set the created search button equal to the search button on the google page, and set it to the first one (i.e. the one at the top of the page (there are 2), to the index is 0
                        btnSearch = objDoc.all.item(txtSubmitName.Text, 0)
                        ' Click the button
                        btnSearch.click()

                        ' Remember that there are more elements that you can create, like htmlInputImage. HTMLAnchorElement and many more

                    End If

                Next  ' end interation through html oject collection
            Catch ex As Exception
                ' If there is an error, it is for the reason stated below - you can change the code to be more robust or to handle different pages.
                MessageBox.Show("It is likely that your submit does not exist or has no name attribute. Check the HTML source.", "No name att. or no submit available", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

            End Try

        End Sub

This will work for filling in an input and submitting on Google, but other pages will need you to change name attributes and perhaps add in some other code to get that specific page to work properly.

That's basically it, so now you can automate form submission in VB.NET.

Cheers,

Renegade

** Download the source code here

 

 

 

 Print   
     
Renegade Minds Web Media Basics  | 5.1 Audio on the Web | Email Avenger  | Web Form Submitter | HTTP Headers | HTTP_USER_AGENT

  Search

Copyright 2008 by Renegade Minds   |   Privacy Statement   |   Terms of Use
Renegade Minds