2016-09-29 37 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