GetElementsbyClassname: Open IE vs. MSXML2 Methods

后端 未结 1 643
醉梦人生
醉梦人生 2020-12-11 08:42

The following macro works just fine. It opens an instance of IE and uses the \"getelementsbyclassname\" method to return the expected value for \"my_rate\". However when I

相关标签:
1条回答
  • 2020-12-11 09:29

    The error message is pretty straightforward:

    GetElementsByClassName is not a method available in the Microsoft XML, v6.0 library.

    You can review the available methods, here:

    http://msdn.microsoft.com/en-us/library/aa926433.aspx

    And while I can't find a similar documentation link, if you enable referenc to the MSHTML library, you can review there to confirm likewise, there is not GetElementsByClassName method. This is a method that is available to IE automation but not to HTML or DOMDocument.

    enter image description here

    UPDATED

    While this may not resolve your problem, I put it here in case it helps others with IE8. It seems to be working for this purpose, but may need to be refined.

    Option Explicit
    
    Sub BankRate_Rate_Retrieval()
    Dim my_url As String
    Dim html_doc As Object 'HTMLDocument
    Dim xml_obj As Object 'MSXML2.DOMDocument
    Dim my_rate As String
    
    my_url = "http://www.bankrate.com/funnel/mortgages/mortgage-results.aspx?market=321&loan=150000&perc=20&prods=2&points=0"
    
    Set html_doc = CreateObject("htmlfile")
    Set xml_obj = CreateObject("MSXML2.XMLHTTP")
    
    xml_obj.Open "GET", my_url, False
    xml_obj.send
    html_doc.body.innerhtml = xml_obj.responseText
    
    'attempt to replicate the GetElementsByClassName for IE8
    
    my_rate = IE8_GetElementsByClassName(html_doc.body, "br-col-2 br-apr", 1).GetElementsByTagName("div")(0).InnerText
    
    MsgBox my_rate
    
    
    Set xml_obj = Nothing
    Set html_doc = Nothing
    
    
    End Sub
    
    Function IE8_GetElementsByClassName(html As Object, className As String, Optional Position As Integer)
    'Function to return an array of matching classname elements
    ' or if specified will return a single HTMLElement by Position index
    
    Dim eleDict As Object
    Dim ele as Variant
    Set eleDict = CreateObject("Scripting.Dictionary")
    For x = 0 To html.all.Length - 1
        Set ele = html.all(x)
        If ele.className = className Then
            'Debug.Print i & vbTab & x & vbTab & ele.InnerText
            Set eleDict(i) = ele
            i = i + 1
        End If
    Next
    
    If Position = Empty Then
        IE8_GetElementsByClassName = eleDict.Items
    Else
        Set IE8_GetElementsByClassName = eleDict(Position)
    End If
    Set eleDict = Nothing
    End Function
    
    0 讨论(0)
提交回复
热议问题