2016-09-29 48 views
0

我有一小段代碼試圖在網站中搜索並告訴是否有任何結果。Excel VBA Web Scraping - 對象所需的錯誤

它對我很好(@India),但對我的同事(@US)不起作用。他檢查了2臺電腦,但得到同樣的錯誤。

我點擊搜索按鈕後,ie.Document變得完全空白,也ie.Document.ChildNodes.Length返回0

Sub iTest() 
    Call ProcessRecord("First_Name", "Middle_Name", "Last_Name", "1/1/2015") 
End Sub 
Private Function ProcessRecord(fName As String, mName As String, lName As String, dob As String) As Boolean 
    Dim results As String, idx% 

    Dim ie As New InternetExplorer 
    ie.Visible = True 

    Navigate ie, "http://ws.ocsd.org/ArrestWarrants/default.aspx" 
    Do While ie.Busy Or ie.ReadyState <> READYSTATE_COMPLETE 
     Application.Wait Now + TimeSerial(0, 0, 1) 
    Loop 
    Do While ie.Busy Or ie.Document.ReadyState <> "complete" 
     Application.Wait Now + TimeSerial(0, 0, 1) 
    Loop 

    ' fill values in webpage 
    ie.Document.getElementById("FirstName").Value = fName 
    ie.Document.getElementById("MiddleName").Value = mName 
    ie.Document.getElementById("LastName").Value = lName 
    ie.Document.getElementById("DOB").Value = dob 

    ' click on search button 
    ie.Document.getElementById("btnSearch").Click 

    ' wait for results 
    Do While ie.Busy Or ie.ReadyState <> READYSTATE_COMPLETE 
     Application.Wait Now + TimeSerial(0, 0, 1) 
    Loop 
    Do While ie.Busy Or ie.Document.ReadyState <> "complete" 
     Application.Wait Now + TimeSerial(0, 0, 1) 
    Loop 

    ' check results 
    results = ie.Document.getElementById("lblResults").innerText   '<< It gives Object Required error here. Because ie.Document has no element after I click on search button. 

    If results = "No Results Found." Then 
     MsgBox "Not found", vbExclamation 
    Else 
     MsgBox "Found", vbExclamation 
    End If 

    ie.Quit 
End Function 
+0

有人可以幫忙嗎? – Tejas

回答

-1

你試過以下?

Do While ie.Busy Or ie.ReadyState <> READYSTATE_COMPLETE DoEvents Loop

似乎是爲我工作的,可能只是在爲你的朋友美國互聯網連接的延遲。

+0

我的代碼中已經有了這個循環。你有我沒有的DoEvent。它有什麼區別? – Tejas