Click here to Skip to main content
15,885,891 members
Articles / Programming Languages / Visual Basic

Automation Tool for Web Activities

Rate me:
Please Sign up or sign in to vote.
4.87/5 (18 votes)
5 Jan 2011CPOL3 min read 58.6K   331   27   26
You can reduce human efforts by automating the work.

Background

Following my post about Live Score [Cricket] on GIT, I got a request to publish the code for doing the same. And here I am, starting with my first article. I hope that you will find it useful in one or the other way.

Introduction

There is nothing much to be done, but it is found that not many of us know the way - how to do it. I am showing you how you can update "your" post automatically in CodeProject. There can be many other uses of it, but I recently used it for this task.

Using the Code

It consist of 2 steps:

  1. Get the content to be posted
  2. Post the content

This code is required to know whether the page is loaded.

VB.NET
Dim proceed As Boolean

Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, _
  ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) _
  Handles WebBrowser1.DocumentCompleted
  proceed = True
End Sub

And here is the function that can be used to browse a page, or to post some data to a page.

VB.NET
Public Sub FillForm(ByRef wbrowser As System.Windows.Forms.WebBrowser, _
  ByVal url As String, Optional ByVal keys() As String = Nothing, _
  Optional ByVal values() As String = Nothing, _
  Optional ByVal submit As String = "")
  Dim obj As HtmlElement
  Dim objcol As System.Windows.Forms.HtmlElementCollection

  proceed = False
  wbrowser.Navigate(url)
  While Not proceed
    Application.DoEvents()
  End While
  proceed = False

  If keys Is Nothing Or values Is Nothing Then Exit Sub

  If keys.Length = values.Length Then
    For i As Integer = 0 To keys.Length - 1
      obj = wbrowser.Document.GetElementById(keys(i))
      If obj IsNot Nothing Then obj.SetAttribute("value", values(i))
    Next

    If submit <> "" Then
      If submit = "true" Then
        objcol = wbrowser.Document.GetElementsByTagName("input")
        For Each objx In objcol
          If objx.GetAttribute("Type").ToLower = "submit" Then
            obj = objx
            Exit For
          End If
        Next
      Else
        obj = wbrowser.Document.GetElementById(submit)
      End If
      If obj IsNot Nothing Then
        obj.InvokeMember("Click")
      End If
    End If
  End If
  proceed = False
End Sub

To browse a page, you can use this function like:

FillForm(WebBrowser1, "www.google.com") 

My application uses Yahoo Cricket to get the latest score. So I get the page:

FillForm(WebBrowser1, _
http://cricket.yahoo.com/cricket-live-score-south-africa-vs-india_11992)  

When the page is loaded, I have to look for the score. I checked the page source for that and found that tab1 and header-main contains the main score.

So I use the following to get the score content:

VB.NET
Dim content As String = WebBrowser1.Document.GetElementById_
	("header-main").InnerText.Trim & vbCrLf & vbCrLf & _
	WebBrowser1.Document.GetElementById("tab1").InnerText

This is how I get the content to be posted.


Now we have to post it to CodeProject. For that, I get the edit link of my post, which is - http://www.codeproject.com/script/Forums/Edit.aspx?fid=1580229&select=3719714&floc=/Forums/1580229/General-Indian-Topics.aspx&action=m.

Now with the above function, it is a piece of cake to get it posted.

VB.NET
FillForm(WebBrowser1, My.Settings.EditURL, New String() _
	{"ctl00_MC_Subject", "ctl00_MC_ContentText_MessageText"}, _
	New String() {"Live Score [Cricket]", content}, "ctl00_MC_submit")

My.Settings.EditURL contains the edit URL of my post. Again with page source, I found the IDs of the required controls:

  • ctl00_MC_Subject
  • ctl00_MC_ContentText_MessageText
  • ctl00_MC_submit

As you can see, keys contains the control IDs which are to be edited and values contain the values for those controls in the same order. submit is the ID of the button to post the message. I have a special case for this parameter, if submit="true", then it finds the button on the page and clicks it. This can be useful when there is just one button on page having no ID.

It works as follows: It looks for controls in the keys and assigns them values from values. After assigning all the values, it looks for the submit button to post it.

Points of Interest

This can be used in many ways. This is just what and how you can do it. Just a wise word - use it for good, don't use it to spam.

I know that this doesn't deal with any message box, or script error coming from page. And there can be many other cases to be considered.

There are a wide scope of modifications, but so far, I am fine with this one function only. It worked whenever I required it. Still, your suggestions are welcome. Please keep me posted if you find a good use of it, or a modification.

History

  • No changes have been made to the code at this stage (Jan 2011)
  • [201101051220] Included code

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
Working since 2006 on VBA, VB6, VB.Net, C#, ASP.Net, MSSQL




  • Courage is not the absence of fear, but rather the judgement that something is more important than fear.
  • The fear of suffering is worse than the suffering itself.
  • People need not fear the unknown if they are capable of achieving what they need and want.
  • Every blessing ignored becomes a curse.
  • Sometimes what's in your head isn't as crazy as you think.
  • We never really grow up, we only learn how to act in public.
  • You can make very bad teams with very good individuals.
  • Admitting mistakes means you have a sense of responsibility in your actions and that shows you are more matured than almost anyone. -Nithin


Comments and Discussions

 
GeneralMy vote of 1 Pin
Harmanjeet Singh20-Sep-11 3:45
Harmanjeet Singh20-Sep-11 3:45 
GeneralMy vote of 5 Pin
Prasanta_Prince25-May-11 21:51
Prasanta_Prince25-May-11 21:51 
GeneralMy vote of 5 Pin
thatraja5-Apr-11 7:55
professionalthatraja5-Apr-11 7:55 
Generalnice - have 5 Pin
Pranay Rana25-Jan-11 2:02
professionalPranay Rana25-Jan-11 2:02 
GeneralMy vote of 5 Pin
Abhinav S22-Jan-11 22:59
Abhinav S22-Jan-11 22:59 
GeneralWould be better Pin
Richard MacCutchan21-Jan-11 3:12
mveRichard MacCutchan21-Jan-11 3:12 
GeneralMy Vote of 5 Pin
RaviRanjanKr18-Jan-11 18:15
professionalRaviRanjanKr18-Jan-11 18:15 
GeneralRe: My Vote of 5 Pin
Prerak Patel18-Jan-11 20:26
professionalPrerak Patel18-Jan-11 20:26 
GeneralMy vote of 5 Pin
MDNadeemAkhter11-Jan-11 1:28
MDNadeemAkhter11-Jan-11 1:28 
GeneralRe: My vote of 5 Pin
Prerak Patel11-Jan-11 3:36
professionalPrerak Patel11-Jan-11 3:36 
GeneralMy vote of 5 Pin
prasad025-Jan-11 15:08
prasad025-Jan-11 15:08 
Generalanother way... [modified] Pin
Selvin5-Jan-11 2:01
Selvin5-Jan-11 2:01 
GeneralRe: another way... Pin
Prerak Patel5-Jan-11 2:05
professionalPrerak Patel5-Jan-11 2:05 
GeneralRe: another way... Pin
Selvin5-Jan-11 4:37
Selvin5-Jan-11 4:37 
GeneralMy vote of 5 Pin
Sandesh M Patil5-Jan-11 0:10
Sandesh M Patil5-Jan-11 0:10 
GeneralMy vote of 5 Pin
Baji Jabbar5-Jan-11 0:02
Baji Jabbar5-Jan-11 0:02 
GeneralRe: My vote of 5 Pin
Prerak Patel5-Jan-11 0:03
professionalPrerak Patel5-Jan-11 0:03 
GeneralRe: My vote of 5 Pin
Baji Jabbar5-Jan-11 0:06
Baji Jabbar5-Jan-11 0:06 
GeneralRe: My vote of 5 Pin
Richard MacCutchan21-Jan-11 3:06
mveRichard MacCutchan21-Jan-11 3:06 
GeneralRe: My vote of 5 Pin
Baji Jabbar21-Jan-11 7:05
Baji Jabbar21-Jan-11 7:05 
GeneralRe: My vote of 5 Pin
Richard MacCutchan21-Jan-11 9:11
mveRichard MacCutchan21-Jan-11 9:11 
GeneralGood One Pin
Baji Jabbar5-Jan-11 0:02
Baji Jabbar5-Jan-11 0:02 
GeneralRe: Good One Pin
Prerak Patel5-Jan-11 0:08
professionalPrerak Patel5-Jan-11 0:08 
GeneralRe: Good One Pin
Baji Jabbar5-Jan-11 0:10
Baji Jabbar5-Jan-11 0:10 
GeneralGood article prerak, But Pin
Hiren solanki4-Jan-11 23:54
Hiren solanki4-Jan-11 23:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.