问题描述
我正在尝试将我的一些工作 VBA 代码转换为 VBScript,但在尝试使用 getElementsByClassName 方法时在 VBScript 中不断出现错误.完整代码如下:
I am trying to convert some of my working VBA code to VBScript, but keep getting errors in VBScript when trying to use the getElementsByClassName method. Here's the full code:
option explicit
Dim XMLPage, html
Set XMLPage = WScript.CreateObject("MSXML2.XMLHTTP")
Set html= CreateObject("HTMLFile")
XMLPage.Open "GET", "https://www.hltv.org/stats/matches/mapstatsid/48745/immortals-vs-dignitas", False
Wscript.Sleep 50
XMLPage.send
Wscript.Sleep 50
If XMLPage.Status <> 200 Then MsgBox XMLPage.statusText
html.Open
html.write XMLPage.responseText
html.Close
'msgbox html.getElementsByTagName("tbody")(0).innertext'WORKS
msgbox html.getElementsByClassName("match-info-box-con")(0).innertext'DOESNT WORK
最后一行代码是出现以下错误的地方:
The last line of code is where the following error occurs:
如果我将其注释掉并运行它来搜索标签名称(上面一行中的代码) - 它工作正常没问题.
If I comment that out and run it to search for a tag name instead (code on the line above) - it works fine no problem.
据我所知,我怀疑这与变量 html
的声明方式有关.getElementsByClassName
来自 IHTMLElement6
- 但我不确定如何让它在 VBScript 中工作.
I suspect it has something to do with how the variable html
is declared, as from what I understand. getElementsByClassName
comes from IHTMLElement6
- but I am unsure on how to get this to work in VBScript.
推荐答案
MSHTML 的行为取决于它的实例化方式 - 它根据其早期或晚期绑定(它严重依赖于 IDispatch
).
MSHTML behaves differently depending on how it was instantiated - it exposes different interfaces depending on whether or not its early or late bound (its heavily reliant on IDispatch
).
您是后期绑定,并且没有公开 getElementsByClassName
的接口可用.
You are late binding and no interface exposing getElementsByClassName
is available.
您可以遍历 document.all()
并查看每个 item.className
.
You can loop over document.all()
and look at each item.className
.
这篇关于不支持 VBScript GetElementsByClassName?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!