Click here to Skip to main content
Click here to Skip to main content

Automation Tool for Web Activities

, 5 Jan 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

  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.

  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:

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.

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)

Share

About the Author

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



  • 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 PinmemberHarmanjeet Singh20-Sep-11 3:45 
GeneralMy vote of 5 PinmemberPrasanta_Prince25-May-11 21:51 
GeneralMy vote of 5 Pinmvpthatraja5-Apr-11 7:55 
Generalnice - have 5 PinmemberPranay Rana25-Jan-11 2:02 
GeneralMy vote of 5 PinmvpAbhinav S22-Jan-11 22:59 
GeneralWould be better PinmvpRichard MacCutchan21-Jan-11 3:12 
GeneralMy Vote of 5 PinmemberRaviRanjankr18-Jan-11 18:15 
GeneralRe: My Vote of 5 PinmemberPrerak Patel18-Jan-11 20:26 
GeneralMy vote of 5 PinmemberMDNadeemAkhter11-Jan-11 1:28 
GeneralRe: My vote of 5 PinmemberPrerak Patel11-Jan-11 3:36 
GeneralMy vote of 5 Pinmemberprasad025-Jan-11 15:08 
Generalanother way... [modified] PinmemberSelvin5-Jan-11 2:01 
GeneralRe: another way... PinmemberPrerak Patel5-Jan-11 2:05 
GeneralRe: another way... PinmemberSelvin5-Jan-11 4:37 
GeneralMy vote of 5 PinmemberSChristmas5-Jan-11 0:10 
GeneralMy vote of 5 PinmembermaB_J5-Jan-11 0:02 
GeneralRe: My vote of 5 PinmemberPrerak Patel5-Jan-11 0:03 
GeneralRe: My vote of 5 PinmembermaB_J5-Jan-11 0:06 
GeneralRe: My vote of 5 PinmvpRichard MacCutchan21-Jan-11 3:06 
GeneralRe: My vote of 5 PinmembermaB_J21-Jan-11 7:05 
GeneralRe: My vote of 5 PinmvpRichard MacCutchan21-Jan-11 9:11 
GeneralGood One PinmembermaB_J5-Jan-11 0:02 
GeneralRe: Good One PinmemberPrerak Patel5-Jan-11 0:08 
GeneralRe: Good One PinmembermaB_J5-Jan-11 0:10 
GeneralGood article prerak, But PinmemberHiren Solanki4-Jan-11 23:54 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.141022.2 | Last Updated 5 Jan 2011
Article Copyright 2011 by Prerak Patel
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid