GetElementsbyClassname: Open IE vs. MSXML2 Methods

时光怂恿深爱的人放手 提交于 2019-11-28 12:35:51

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.

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!