访问vba IE getElementById“需要对象”错误424

时间:2019-07-09 23:57:25

标签: access-vba

这与此问题有关

scrape data from a table on a website without having to search for tags

此代码直到几天前都起作用,此后唯一的更改是更改为Windows 10,但这应该不会影响它,对吗?

而且,它似乎刮了第一个记录,然后给出了错误。但是,如果我单击DEBUG(调试),然后单击“ STEP OUT”(退出),它将起作用并转到下一条记录,我再次必须单击“ DEBUG(调试)”,“ STEP OUT”(退出)等。 81次。

还有其他函数被调用,它们只是将更多的内容抓取到一个表中,我不认为这是问题所在,但是我可以根据需要添加它们。

给出错误424的行是

If .Document.getElementById("middleContent_lbType").outerHTML Like "*General Acute Care Hospital*" Then

这是代码

Public Sub VisitPages()

DoCmd.RunSQL "DELETE FROM ScrapedFacs"

    AutoID = 1
    Dim ie As New InternetExplorer
    'Set ie = New InternetExplorerMedium
    With ie
        .Visible = False
        .navigate "http://healthapps.state.nj.us/facilities/acSetSearch.aspx?by=county"

        While .Busy Or .ReadyState < 4: DoEvents: Wend

        With .Document
            .querySelector("#middleContent_cbType_1").Click
            .querySelector("#middleContent_cbType_4").Click
            .querySelector("#middleContent_btnGetList").Click
        End With

        While .Busy Or .ReadyState < 4: DoEvents: Wend

        Dim list As Object, i  As Long
        Set list = .Document.querySelectorAll("#main_table [href*=doPostBack]")
        For i = 0 To list.Length - 1
            list.Item(i).Click

            While .Busy Or .ReadyState < 4: DoEvents: Wend

If .Document.getElementById("middleContent_lbType").outerHTML Like "*General Acute Care Hospital*" Then
FacType = "General Acute Care Hospital"
ElseIf .Document.getElementById("middleContent_lbType").outerHTML Like "*Psychiatric Hospital*" Then
FacType = "Psychiatric Hospital"
End If


Address = Replace(Replace(Replace(.Document.getElementById("middleContent_lbAddress").outerHTML, "<span id=" & Chr(34) & "middleContent_lbAddress" & Chr(34) & ">", ""), "<br>", ", "), "</span>", "")

            WriteTable .Document.getElementsByTagName("table")(3), .Document.getElementById("middleContent_lbName_county").innerText


            'do stuff with new page
            .Navigate2 .Document.URL             '<== back to homepage
            While .Busy Or .ReadyState < 4: DoEvents: Wend
            Set list = .Document.querySelectorAll("#main_table [href*=doPostBack]") 'reset list (often required in these scenarios)

        Next
       ' Stop                                     '<== Delete me later
        .Quit '<== Remember to quit application
    End With
End Sub

1 个答案:

答案 0 :(得分:2)

您面临的问题是因为您的代码运行速度太快了

所以在行前添加睡眠

Sleep 1
...Document.getElementById("middleContent_lbType")..

或者开始检查您是否有返回值

Set obj = .Document.getElementById("middleContent_lbType")
If obj is Nothing:
   Sleep 1
   Set obj = .Document.getElementById("middleContent_lbType")
End If

If obj.outerHTML Like "*General Acute Care Hospital*" Then
...