GetElementsbyClassname:打开IE与MSXML2方法

时间:2014-05-05 15:41:42

标签: vba internet-explorer excel-vba getelementsbyclassname excel

以下宏工作正常。它打开IE的一个实例,并使用“getelementsbyclassname”方法返回“my_rate”的预期值。但是,当我运行使用“MSXML2”方法的第二个宏时,宏在注释行上失败,并且出现“运行时错误438:对象不支持此属性或方法”错误。为什么“Open IE”方法有效,但“MSXML2”方法失败了我的代码?我正在使用IE 11运行。我还有第二个宏的Microsoft HTML对象库的引用集,但它似乎没有什么区别。在此先感谢您向我解释。

Sub BankRate_Rate_Retrieval()
    my_url = "http://www.bankrate.com/funnel/mortgages/mortgage-results.aspx?market=321&loan=150000&perc=20&prods=2&points=0"   
    Set ie = CreateObject("InternetExplorer.Application")

    With ie
        .Visible = True
        .Navigate my_url
        .Top = 50
        .Left = 530
        .Height = 400
        .Width = 400
    End With

    Do Until Not ie.Busy And ie.readyState = 4
        DoEvents
    Loop

    my_rate = ie.Document.getelementsbyclassname("br-col-2 br-apr")(1).getElementsByTagName("div")(0).innertext
End Sub


Sub BankRate_Rate_Retrieval()
    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
    Set xml_obj = Nothing

    my_rate = html_doc.body.getelementsbyclassname("br-col-2 br-apr")(1).getElementsByTagName("div")(0).innertext

'运行时错误438:对象不支持此属性或方法出现在上一行

End Sub

编辑:D. Zemens的图书馆截图

enter image description here

1 个答案:

答案 0 :(得分:2)

错误信息非常简单:

GetElementsByClassName不是Microsoft XML,v6.0库中提供的方法。

您可以在此处查看可用的方法:

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

虽然我找不到类似的文档链接,但如果您启用参考MSHTML库,您可以在那里查看同样确认,没有GetElementsByClassName方法。这是一种可用于IE自动化但不适用于HTML或DOMDocument的方法。

enter image description here

<强>已更新

虽然这可能无法解决您的问题,但我把它放在这里,以防其他人使用IE8。它似乎正在为此目的而努力,但可能需要进一步完善。

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