Results 1 to 5 of 5

Thread: [RESOLVED] newbie - code design suggestion - sub vs function

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    13

    Resolved [RESOLVED] newbie - code design suggestion - sub vs function

    Hello,
    I am working on a personal project to make my life easier.
    I am automating a javascript web form submission process using webbrowser.
    I would like to use something without a UI but don't think httpresponse etc would do all that is needed at a newbie level of understanding.

    My code works but I think is not designed correctly. I think I should be breaking each step into its own sub or function.

    In its current state I think every time a page is refreshed/redrawn all the code is running which makes causes endless looping of clicking a link which is on its own destination page. What that means is click the link and the page refreshes with new information but does not navigate to a new page. So the code looks for the link and selects it over and over and over .... This I think needs to be subbed or done as a function instead.

    Here's the code. Please suggest or give guidance with specific examples (I am a newbie and only touch programming once a year or so.)

    Thanks in advance

    Code:
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Part 1: Load elogging login page in Form_Load event  
            WebBrowser1.Navigate("https://www.somesite.com/elog/login.do")
        End Sub
    
        Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
            'Part 2: Locate the Username TextBox and automatically input your username  
            '<INPUT class=yreg_ipt id=username maxLength=96 size=17 name=login> 
            Dim clicked As Integer
            clicked = 0
            Dim theElementCollection As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("Input")
            For Each curElement As HtmlElement In theElementCollection
                Dim controlName As String = curElement.GetAttribute("name").ToString
                If controlName = "userId" Then
                    curElement.SetAttribute("Value", "User_name")
                End If
            Next
    
            'Part 3: Locate the Password TextBox and automatically input your password  
            '<INPUT class=yreg_ipt id=passwd type=password maxLength=64 size=17 value="" name=passwd>  
            theElementCollection = WebBrowser1.Document.GetElementsByTagName("Input")
            For Each curElement As HtmlElement In theElementCollection
                Dim controlName As String = curElement.GetAttribute("name").ToString
                If controlName = "passwd" Then
                    curElement.SetAttribute("Value", "password")
                End If
            Next
    
            'Part 4: Locate the "Sign In" Button and automatically click it  
            theElementCollection = WebBrowser1.Document.GetElementsByTagName("a")
            For Each curElement As HtmlElement In theElementCollection
                If curElement.GetAttribute("href").Equals("javascript:doLogin()") Then
                    curElement.InvokeMember("click")
                    'Javascript has a click method for we need to invoke on the current submit button element.  
                End If
            Next
    
            Dim navigateurl = WebBrowser1.Url.ToString
            MsgBox(navigateurl)
    
            'Part 5: Locate the "View Case" link and automatically click it  
            For Each curElement As HtmlElement In theElementCollection
                Dim controlText As String = curElement.GetAttribute("InnerText").ToString
                If controlText = "View Support Request" Then
                    'Dim url As String = curElement.GetAttribute("href")
                    clicked = clicked + 1
                    If (clicked < 2) Then
                        curElement.InvokeMember("click")
                        ' Click the hyperlin
                    End If
                End If
            Next
    
            'Part 6 locate the support request ID text box and enter in case number
            theElementCollection = WebBrowser1.Document.GetElementsByTagName("Input")
            For Each curElement As HtmlElement In theElementCollection
                Dim controlName As String = curElement.GetAttribute("name").ToString
                If controlName = "caseId" Then
                    curElement.SetAttribute("Value", "C455603")
                End If
            Next
    
            'Part 7 search for the case
            theElementCollection = WebBrowser1.Document.GetElementsByTagName("a")
            For Each curElement As HtmlElement In theElementCollection
                If curElement.GetAttribute("href").Equals("javascript:doSearch()") Then
                    curElement.InvokeMember("click")
                    'Javascript has a click method for we need to invoke on the current submit button element.  
                End If
            Next
    
            'Part 8: Locate the "View Case" link and automatically click it  
           theElementCollection = WebBrowser1.Document.GetElementsByTagName("a")
            For Each curElement As HtmlElement In theElementCollection
                If curElement.GetAttribute("href").Equals("javascript:doDetails(0)") Then
                    curElement.InvokeMember("click")
                    'Javascript has a click method for we need to invoke on the current submit button element.  
                End If
            Next
    
    
    
        End Sub
    
    End Class

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: newbie - code design suggestion - sub vs function

    Using a Sub or Function in this case would not make any difference. Your problem is that all of your code is in the DocumentCompleted event handler and that event gets raised every time the web browser loads a page. So if your code makes the web browser load another page then your code is just going to get executed again and again and again. I'm guessing though that when your code clicks a button and another page is loaded, the URL for the new page will be different to the previous page's URL so your code should use an IF statement to check which URL you are currently on and perform the relevant actions.
    Very rough example:
    vb Code:
    1. Private Sub Document_Completed Event 'etc
    2.    If WebBrowser1.URL.ToString = "THE SIGN IN URL GOES HERE" Then
    3.      'Put code here that finds the username + password box and clicks the sign in button
    4.    ElseIf WebBrowser1.URL.ToString = "WHATEVER URL THAT YOU GET TO AFTER SIGN IN GOES HERE"
    5.      'Do whatever you do once you have signed in
    6.    End If
    7. End Sub
    Depending on what the URL is once you have finished your work, you might also need to have a boolean value that you set to True once all of the work is finished and you check that at the start of the Document_Completed event handler - if it is True then you do nothing, if not then you move on to the other IF statements I showed an example of
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    13

    Re: newbie - code design suggestion - sub vs function

    Thanks for that. The problem is that from the login page to the next page is ok the url changes but from then on the url does not change but the page contents do.

    There page is all done with javascript. There are 5 links at the top which one gets selected.
    Then the body of teh page is draw depending on what was selected. In this case a form, but the url does not change. The 5 links are still there. I then fill the form and select search. The form with my criteria stays and the page redraws with the results listed below the form.

    Selecting the result that you want the form is replaced with another form. Which may be impossible to find something that is always unique on it. Again the url has not changed but the page has been refreshed with new or additional information.

    I tried a counter or Boolean but as long as it falls in the Document Completed event it will be reset each refresh.

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    13

    Re: newbie - code design suggestion - sub vs function

    I found something that is unique.
    It is a background image of a table cell. I have tried various combination of the following with no luck.

    I have worked it out. For those interested.
    Code:
    Dim theElementCollection As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("Input")
            theElementCollection = WebBrowser1.Document.GetElementsByTagName("td")
            For Each curElement As HtmlElement In theElementCollection
                If curElement.GetAttribute("background").Equals("/elog/images/point_1.gif") Then
    
                    Step4()
    
                End If
            Next
    Last edited by emitremmah; Jul 31st, 2010 at 10:58 AM.

  5. #5
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [RESOLVED] newbie - code design suggestion - sub vs function

    I tried a counter or Boolean but as long as it falls in the Document Completed event it will be reset each refresh
    What you need to do then is check the value of it first before you increment it. So basically you would declare an Integer at class level (ie outside of an event handler or Sub/Function) and use this to keep track of which stage you are currently at. After you complete each stage you add 1 to the counter so that the next time the DocumentCompleted event is raised your code will know which bit to execute.

    E.g:
    vb Code:
    1. Public Clas Form1
    2.  
    3.   'Declare our counter at class level
    4.   Private Counter As Integer = 0
    5.  
    6.   Private Sub Document_Completed etc etc
    7.       If Counter = 0 Then
    8.           'Do sign in stuff here
    9.           'Add 1 to the Counter
    10.           Counter += 1
    11.       ElseIf Counter = 1 Then
    12.          'Do stuff here that you would do after sign in
    13.          Counter += 1
    14.       ElseIf Counter = 2 Then
    15.          'Do stuff here you would do the next time the page is reloaded
    16.          Counter += 1
    17.       End If
    18.   End Sub
    19.  
    20. End Class

    Of course you need to make sure you reset the counter to 0 if you start this process again
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width