我在vb.net webbrowser中有一个需要自动化的屏幕抓取程序。基本上它登录到一个网站,并填写表格,然后让我到我需要刮的屏幕。这没有问题,我不知道为什么它不会循环或如何让它循环。任何输入将不胜感激!
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