0

I have a screen scraper program in vb.net webbrowser that needs to be automated. Essentially it logs into a website and fills out forms that then gets me to the screen I need to scrape. This WORKS with no problems however, I am not sure why it will not loop or how to get it to loop. Any input would be appreciated!

Private Sub oschner()  

    For (loopcount = 0 to acctData.Count - 1)

        'Dim sixthStepHandler As WebBrowserDocumentCompletedEventHandler =
        '        Sub(wsender As Object, we As WebBrowserDocumentCompletedEventArgs)

        '            'If the Then WebBrowser hasn't finished loading, do not continue.
        '            If webBrowser1.ReadyState <> WebBrowserReadyState.Complete Then Return

        '            'Remove the event handler to avoid the code being called twice.
        '            RemoveHandler webBrowser1.DocumentCompleted, sixthStepHandler

        '        End Sub

        'Scraping the Screen
        Dim fifthStepHandler As WebBrowserDocumentCompletedEventHandler =
            Sub(wsender As Object, we As WebBrowserDocumentCompletedEventArgs)
                Dim x As Integer = loopcount
                'If the Then WebBrowser hasn't finished loading, do not continue.
                If webBrowser1.ReadyState <> WebBrowserReadyState.Complete Then Return

                'Remove the event handler to avoid the code being called twice.
                RemoveHandler webBrowser1.DocumentCompleted, fifthStepHandler
                Dim html As String = webBrowser1.DocumentText.ToString
                If html.Contains("Please Correct and Resubmit") Then
                    Console.WriteLine(x.ToString + ":    " + acctData(x)(1) + "    Bad Account")
                    AddHandler webBrowser1.DocumentCompleted, fifthStepHandler
                    'Form of exit to rerun with different criteria for the same acccount
                Else
                    'PARSING DOCUMENT TO GET VARIABLES
                    Console.WriteLine(x.ToString + ":    " + acctData(x)(1) + "    Good Account")
                    Dim InsID, EligDate, InsName, InsPhone As String
                    Dim totalTables As HtmlElementCollection = webBrowser1.Document.GetElementsByTagName("table")
                    For Each table As HtmlElement In totalTables
                        For Each row As HtmlElement In table.GetElementsByTagName("td")
                            For Each cell As HtmlElement In row.All
                                Try
                                    Dim IDcount As Integer
                                    Dim tempstr As String
                                    tempstr = cell.InnerText
                                    'GETTING SUBSCRIBER ID
                                    If IDcount = 9 Then
                                        InsID = tempstr
                                        'GETTING ELIGIBILITY DATE
                                    ElseIf IDcount = 51 Then
                                        EligDate = tempstr
                                        'GETTING INSNAME
                                    ElseIf IDcount = 82 Then
                                        If tempstr = "" Then
                                            InsName = "Molina Medicaid Solutions"
                                        Else
                                            InsName = tempstr
                                        End If
                                        'GETTING INSPHONE
                                    ElseIf IDcount = 85 Then
                                        InsPhone = tempstr
                                    End If
                                    IDcount = IDcount + 1
                                Catch
                                End Try
                            Next
                        Next
                    Next
                    Console.WriteLine(acctData(x)(0).ToString + "    " + EligDate + "    " + InsID + "    " + InsName + "     " + InsPhone)
                    cmd.CommandText = "INSERT INTO [horizontal_pricing].dbo.dat_MedicaidEligibleAccts_LA (fk_Account, EligDate, InsID, InsName, InsPhone) VALUES ('" + acctData(x)(0).ToString + "','" + EligDate + "','" + InsID + "','" + InsName + "','" + InsPhone + "')"
                    'cmd.ExecuteNonQuery()
                End If
                'adding the event handler performing our next step
                'AddHandler webBrowser1.DocumentCompleted, sixthStepHandler
                loopcount = loopcount + 1
                html = ""
            End Sub

        'Looking up Account
        Dim fourthStepHandler As WebBrowserDocumentCompletedEventHandler =
           Sub(wsender As Object, we As WebBrowserDocumentCompletedEventArgs)
               Dim x As Integer = loopcount

           'If the WebBrowser hasn't finished loading, do not continue.
           If webBrowser1.ReadyState <> WebBrowserReadyState.Complete Then Return

           'Remove the event handler to avoid the code being called twice.
           RemoveHandler webBrowser1.DocumentCompleted, fourthStepHandler
               Dim allelements As HtmlElementCollection = webBrowser1.Document.All
               For Each webpageelement As HtmlElement In allelements

                   If webpageelement.GetAttribute("id") = "cmboSearch" Then
                       webpageelement.SetAttribute("value", "NAMEDOB")
                       webpageelement.InvokeMember("onchange")
                   End If

               'Lastname
               If webpageelement.GetAttribute("name") = "txtRecipLastName" Then
                       webpageelement.SetAttribute("value", (acctData(x)(2).ToString))
                   End If
               'Firstname
               If webpageelement.GetAttribute("name") = "txtRecipFirstName" Then
                       webpageelement.SetAttribute("value", (acctData(x)(1).ToString))
                   End If
               'DOB
               If webpageelement.GetAttribute("name") = "cmboDOB" Then
                       webpageelement.SetAttribute("value", (acctData(x)(3).ToString))
                   End If
               'Plan Date
               If webpageelement.GetAttribute("name") = "cmboDOS" Then
                   'webpageelement.SetAttribute("value", "04/25/2016")
               End If
               'Submit Button
               If webpageelement.GetAttribute("id") = "cmdSubmit" Then
                   'adding the event handler performing our next step
                   AddHandler webBrowser1.DocumentCompleted, fifthStepHandler
                       webpageelement.InvokeMember("focus")
                       webpageelement.InvokeMember("click")
                   End If
               Next
           End Sub

        'Going to Account Lookup Page
        Dim thirdStepHandler As WebBrowserDocumentCompletedEventHandler =
                  Sub(wsender As Object, we As WebBrowserDocumentCompletedEventArgs)
                  'If the WebBrowser hasn't finished loading, do not continue.
                  If webBrowser1.ReadyState <> WebBrowserReadyState.Complete Then Return

                  'Remove the event handler to avoid this code being called twice.
                  RemoveHandler webBrowser1.DocumentCompleted, thirdStepHandler

                  'adding the event handler performing our next step
                  AddHandler webBrowser1.DocumentCompleted, fourthStepHandler

                      webBrowser1.Navigate("https://www.lamedicaid.com/sprovweb1/MEVS/mevs.aspx")

                  End Sub

        'Loggin in
        Dim credentialHandler As WebBrowserDocumentCompletedEventHandler =
                    Sub(wsender As Object, we As WebBrowserDocumentCompletedEventArgs)

                    'If the WebBrowser HASN'T finished loading, do not continue.
                    If webBrowser1.ReadyState <> WebBrowserReadyState.Complete Then Return

                    'Remove the event handler to avoid this code being called twice.
                    RemoveHandler webBrowser1.DocumentCompleted, credentialHandler


                        Dim allelements As HtmlElementCollection = webBrowser1.Document.All
                        For Each webpageelement As HtmlElement In allelements
                        'Entering username
                        If webpageelement.GetAttribute("name") = "Login_Id" Then
                                webpageelement.SetAttribute("value", "xxxxxxxx")
                            End If

                        'Entering Password
                        If webpageelement.GetAttribute("name") = "Password" Then
                                webpageelement.SetAttribute("value", "xxxxxxxx")
                            End If

                        'logging in
                        If webpageelement.GetAttribute("name") = "submit_button" Then
                            'adding the event handler performing our next step
                            AddHandler webBrowser1.DocumentCompleted, thirdStepHandler
                                webpageelement.InvokeMember("focus")
                                webpageelement.InvokeMember("click")
                            End If
                        Next
                    End Sub


        'Inputting Provider ID
        Dim loginHandler As WebBrowserDocumentCompletedEventHandler =
                        Sub(wsender As Object, we As WebBrowserDocumentCompletedEventArgs)
                        'If the WebBrowser hasn't finished loading, do not continue.
                        If webBrowser1.ReadyState <> WebBrowserReadyState.Complete Then Return

                        'Remove the event handler to avoid this code being called twice.
                        'RemoveHandler webBrowser1.DocumentCompleted, loginHandler

                        Dim allelements As HtmlElementCollection = webBrowser1.Document.All
                            For Each webpageelement As HtmlElement In allelements
                            'NPI #
                            If webpageelement.GetAttribute("name") = "Provider_Id" Then
                                    webpageelement.SetAttribute("value", "xxxxxxx")
                                End If

                            'Clicking enter to input NPI
                            If webpageelement.GetAttribute("name") = "submit1" Then

                                'Adding the event handler performing our next step.
                                AddHandler webBrowser1.DocumentCompleted, credentialHandler

                                    webpageelement.InvokeMember("focus")
                                    webpageelement.InvokeMember("click")
                                End If
                            Next
                        End Sub

        'Add the event handler performing our first step.
        AddHandler webBrowser1.DocumentCompleted, loginHandler


        webBrowser1.Navigate("https://www.lamedicaid.com/sprovweb1/provider_login/provider_login.asp")

        next
    myreader.Close()
        rows.Clear()
    cols.Clear()
End Sub
  • `I am not sure why it will not loop or how to get it to loop`, this is broad, please provide more details as of now you are asking about debugging the above mentioned code which we do not do. What are you wanting to do with the loop? – Trevor Dec 12 '17 at 14:00
  • @Codexer Although the code is in a loop it will not loop through the process. If I just run it once it will work appropriately. Essentially I am trying to determine if my logic is messed up in thinking that I can simply loop this way or if I will have to try a different method of looping. If I cannot loop through the methods as you are suggesting then how can I deal with the handlers this way? – Cole Perrault Dec 12 '17 at 14:10
  • A loop is not really suitable in this case since it will not respect the `DocumentCompleted` handlers. It will just iterate as fast as possible, adding multiple event handlers and re-navigating to the login page for every iteration. I think your loop works, but it is so fast that you don't notice it. It'd be better if you removed the loop and had a global variable keep track of how many iterations you have left to do. Then you just recall the `oschner()` method in your last `DocumentCompleted` handler and decrement your global variable until it reaches zero. – Visual Vincent Dec 12 '17 at 14:57
  • 1
    @VisualVincent good recommendations, I agree. I know the code he has above you wrote, here's the answer you gave him https://stackoverflow.com/questions/47560085/webbrowser-auto-navigation-event . I would hope he does some digging as this is a debugging issue... Some will post here and there and before you know it, you wrote the application for them. – Trevor Dec 12 '17 at 15:06
  • Here's [a C# solution](https://stackoverflow.com/a/19718530/1768303) which might be useful if you can convert it to VB.NET. – noseratio Dec 12 '17 at 23:54

0 Answers0