getelementsbyclassname Excel vba - 重复调用时出错

getelementsbyclassname Excel vba - error on repeated calls

早上好,

我在使用来自 Excel 的网络抓取时遇到问题,其中 getelementsbyclassname 无法对某些对象执行操作,从而引发 "Object doesn't support this property or method" 错误。

当我输入 getelementsbyclassname 的对象本身是 getelementsbyclassname 方法的结果时,就会出现问题。我不确定为什么,特别是当我在更大的对象上操作时可以获得 class 名称...

这里是代码摘录

''''Boring Variables Declaration I've cut out''''

'Initialise IE
Dim IEApp As New InternetExplorer 
Set IEApp = New InternetExplorer 
IEApp.Visible = True 'JB

'Open page and wait for page to load
  IEApp.navigate ("http://www.anicewebsite.com")
  Do Until IEApp.readyState = READYSTATE_COMPLETE And IEApp.Busy = False
    DoEvents
  Loop

Set HTMLdoc = IEApp.document
Set RefLocation = Sheets("INFO_DUMP").Range("LocationRefCell")
Set trElements = HTMLdoc.getElementsByClassName("basic-details")
For Each trElement In trElements
        'Select the LHS box and extract info
        Set tdElement = trElement.getElementsByClassName("tieredToggle")
        'write start/end locations
        '''''THIS NEXT LINE THROWS AN ERROR'''''
         Data_str = tdElement.getElementsByClassName("title").innerText
        '''''AS DOES'''''
        MyObject=tdElement.getElementsByClassName("title")
         RefLocation.Offset(1, 2).Value = Data_str 
Next 'close tr Loop         

但是,我可以通过

获得'title'对象
For Each trElement In trElements
        Set MyObject=trElement.getElementsByClassName("title")
Next 'close tr Loop

所以错误可能是关于 tdElement(一个 DisHTMLElement 集合)的,我试图附上它的图像但我缺乏声誉(参见 post 末尾的 link) ...

非常感谢您的帮助。

PS。该网页的结构大致上有一个 2 列 table,我用 "basic-details" 隔离了它们的行。第一列是 "tiered toggle" 然后我想要的项目是例如内部文本。 "title"。我需要使用 tieredtoggle,因为每列中的对象都重复了 class names

http://i.stack.imgur.com/1tyb6.png

你可以用它来获取内文。

Data_str = tdElement.getElementsByClassName("title")(0).innerText

您可以输入元素所在位置的索引值,而不是 ("title")(0)。