GetElementsbyClassname:打开IE与MSXML2方法 [英] GetElementsbyClassname: Open IE vs. MSXML2 Methods

查看:180
本文介绍了GetElementsbyClassname:打开IE与MSXML2方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下宏工作正常。它打开IE的一个实例,并使用getelementsbyclassname方法返回my_rate的预期值。但是,当我运行使用MSXML2方法的第二个宏时,该宏在注释的行上失败,并且出现运行时错误438:对象不支持此属性或方法错误。为什么打开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
设置ie = CreateObject(InternetExplorer.Application)

与ie
.Visible = True
.Navigate my_url
.Top = 50
.Left = 530
.Height = 400
.Width = 400
结束

直到不是ie.Busy和ie.readyState = 4
DoEvents
循环

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
设置html_doc = CreateObject(htmlfile)
Se t xml_obj = CreateObject(MSXML2.XMLHTTP)

xml_obj.OpenGET,my_url,False
xml_obj.send
html_doc.body.innerhtml = xml_obj.responseText
设置xml_obj = Nothing

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

'运行时错误438:对象不支持此属性或方法发生在上面的行

End Sub



编辑:图片D. Zemens的图片截图



解决方案

错误信息非常简单:



GetElementsByClassName 不是一种可用的方法Microsoft XML,v6.0库。



您可以在这里查看可用的方法:



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



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





更新



虽然这可能无法解决您的问题,我把它放在这里,如果它帮助其他人使用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

设置html_doc = CreateObject(htmlfile)
设置xml_obj = CreateObject(MSXML2.XMLHTTP)

xml_obj.OpenGET,my_url,False
xml_obj.send
html_doc.body.innerhtml = xml_obj.responseText

'尝试复制IE8的GetElementsByClassName

my_rate = IE8_GetElementsByClassName(html_doc.body,br-col- 2 br-apr,1).GetElementsByTagName(div)(0).InnerText

MsgBox my_rate


设置xml_obj =没有
设置html_doc = Nothing


End Sub

功能IE 8_GetElementsByClassName(html As Object,ClassName As String,可选位置为整数)
'返回匹配的类名元素数组
'的函数,如果指定将返回单个HTMLElement按位置索引

Dim eleDict As Object
Dim ele as Variant
设置eleDict = CreateObject(Scripting.Dictionary)
对于x = 0到html.all.Length - 1
设置ele = html.all(x)
如果ele.className = className然后
'Debug.Print i& vbTab& x& vbTab& ele.InnerText
设置eleDict(i)= ele
i = i + 1
结束如果
下一个

如果Position =空然后
IE8_GetElementsByClassName = eleDict.Items
Else
设置IE8_GetElementsByClassName = eleDict(Position)
End If
设置eleDict = Nothing
结束函数


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 run the second macro which uses the "MSXML2" method, the macro fails on the noted line and a "Run-time error 438: Object doesn't support this property or method" error occurs. Why does the "Open IE" method work, but the "MSXML2" method fail with my code? I am running with IE 11. I also have a reference set to the Microsoft HTML Object Library for the second macro, but it doesn't seem to make a difference. Thanks in advance for explaining this to me.

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

' Run-time error 438: Object doesn't support this property or method occurs on above line

End Sub

Edit: Library Screenshot for D. Zemens

解决方案

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

这篇关于GetElementsbyClassname:打开IE与MSXML2方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆