【问题标题】:On Error not working with IE automation错误无法与 IE 自动化一起使用
【发布时间】:2017-03-01 11:02:50
【问题描述】:

我的代码正在从网站获取信息,但在某些情况下,包含该信息的 HTML 代码不存在,因此我使用 On Error 来处理它。

正如您将在代码中看到的那样,我正在遍历一个列表并获取每一行的信息(大约 700 行)。起初错误发生在第 10 行,然后我添加了On Error GoTo 0。之后它开始被扔到第 13 行。

我的配置已经设置为 Break on Unhandeled Errors

运行时错误号为:

91: 未设置对象变量或 With 块变量。

它发生在“** **”之间的行

Sub GetData_DK()

    Dim IE As New InternetExplorer
    Dim URL As String
    Dim doc As HTMLDocument 'variable for document or data which need to be extracted out of webpage
    Dim onl As String
    Dim sto As String
    Dim pri As String
    FinalRow = tme.Cells(Rows.Count, "G").End(xlUp).Row

    Dim hyper As Workbook
    Set hyper = Workbooks.Open("path")
    FinalRowH = hyper.Sheets("tme").Cells(Rows.Count, "A").End(xlUp).Row
    ThisWorkbook.Activate

    For a = 5 To FinalRow

        onl = ""
        sto = ""
        pri = ""

        For b = 5 To FinalRowH
            If (ThisWorkbook.Sheets("tme").Cells(a, 7).Value = hyper.Sheets("tme").Cells(b, 1).Value) Then
                URL = hyper.Sheets("tme").Cells(b, 3).Value
                Exit For
            End If
        Next b

        IE.navigate URL
        IE.Visible = True

        Do
            If IE.readyState = READYSTATE_COMPLETE Then
                If IE.document.readyState = "complete" Then Exit Do
            End If
            Application.Wait DateAdd("s", 1, Now)
        Loop
        'Application.Wait (Now() + TimeValue("00:00:006")) ' For internal page refresh or loading


        Set doc = IE.document

        On Error Resume Next
        'gets HTLM class containing the value
        **onl = CStr(doc.getElementsByClassName("items-in-stock align-left")(0).innerText)**
        On Error GoTo 0
        If (onl = Chr(160) Or onl = " " Or onl = "   " Or onl = "" Or onl = vbNullString Or Left(onl, 9) = "Forventet") Then
            Cells(a, 8).Value = 0
        Else
            Cells(a, 8).Value = 1
        End If

        On Error GoTo price
        'repeats the process for stores
        sto = CStr(doc.getElementsByClassName("open-cas-tab")(0).innerText)
        sto = Left(sto, InStr(sto, " ") - 1)
        Cells(a, 9).Value = sto

price:
        On Error GoTo 0

        On Error Resume Next
        'repeats the process for price
        pri = CInt(CStr(doc.getElementsByClassName("product-price-container")(0).innerText))
        Cells(a, 10).Value = pri
        On Error GoTo 0
    Next a
End Sub

请让我知道我做错了什么(:

【问题讨论】:

    标签: vba excel internet-explorer error-handling


    【解决方案1】:

    如果使用 getElementsByClassName 没有返回任何元素,那么您可以测试 length 属性,它将为 0。如果您尝试访问第 0 个元素,这比尝试跳过生成的错误更可取,因为您假设您会得到积极的结果。

    所以你可以试试这个:

    Set doc = IE.document
    
    Dim objElements As Object
    Set objElements = doc.getElementsByClassName("items-in-stock align-left")
    
    If objElements.length > 0 Then
        ' some elements are returned - get the text from the first one
        onl = CStr(objElements(0).innerText)
    Else
        ' nothing returned - lets handle it gracefully with no error
        MsgBox "No elements with that class!"
        '... prepare to exit 
    End If
    
    ' do stuff with onl
    

    【讨论】:

    • 这太棒了!非常感谢您分享您的知识,罗宾(:
    猜你喜欢
    • 1970-01-01
    • 2012-02-02
    • 2014-02-23
    • 1970-01-01
    • 2021-06-07
    • 2013-06-07
    • 1970-01-01
    • 1970-01-01
    • 2011-05-06
    相关资源
    最近更新 更多