ASP.NET, Coding

How to Automate Web Browser using VB.NET

Have you ever felt a need to automate data extraction and data posting to websites? If yes then here is a small tutorial which will put you in the right direction. I have exploited the WebBrowser control in VS 2005 to automate the login and logout process on coderewind.com. I have created a test account for this purpose. This is only for educational purpose.

First create a blank VB.NET Windows Application project using Visual Studio 2005. Now add a WebBrowser control on the form. Now add a reference to Microsoft.mhtml library which will give us the functions to control the entire HTML page including individual controls. I have named my WebBrowser control as wb. So keep in mind as I will be calling WebBrowser control as wb in my rest of the article.

This code below show how to use wb to open a website.

wb.Navigate("http://www.coderewind.com", False)
While wb.ReadyState <> WebBrowserReadyState.Complete
Application.DoEvents() 'Process all other windows messages in the queue
End While

Declare an HTMLElement type to get a list of all the elements on that page.

Dim elm As System.Windows.Forms.HtmlElement

Now loop through the entire list of elements

For i As Integer = 0 To wb.Document.All.Count - 1
elm = wb.Document.All.Item(i) 'This will give the current element in the loop

Now you can get the required contol type

If elm.TagName.ToUpper() = "INPUT" Then

And then retrieve the actuall control name

If CType(elm, System.Windows.Forms.HtmlElement).Name = "login_user" Then 'Get User ID Element

Now Set the desired value to that control

CType(elm, System.Windows.Forms.HtmlElement).InnerText = test@test.com

Finally when all values are populated click the submit button of the proper form containing the above set controls.

wb.Document.Forms("formLogin").InvokeMember("submit")

Screenshot of the application

How to Automate Web Browser using VB.NET

Note: You can download the source for this sample application attached with the article.

You Might Also Like