你搞混了
id
和
class
它们是不同的东西,例如
What's the difference between an id and a class?
.
你的元素有
班
-属性,而不是ID。要搜索具有特定类属性的元素,请使用以下函数
getElementsByClassName
请注意,这是一个“复数”函数,它将返回
全部的
具有该特定类属性的元素。即使它只找到一个元素,它也会返回一个可以容纳任意数量元素的数据结构,您需要使用索引来访问其中一个元素。据我所知,在VBA中,这是一个基于0的数组。
如果你总是确信
一
元素已找到,请使用
Set QuestionList = html.getElementsByClassName("fieldgroup ")(0)
Set Questions = QuestionList.Children
或者(但在这种情况下,您需要对问题列表进行不同的定义):
Set QuestionList = html.getElementsByClassName("fieldgroup ")
Set Questions = QuestionList(0).Children
我省略了错误检查,所以如果HTML不包含任何具有该类名的元素,您仍然会收到运行时错误。要编写健壮的代码,您应该添加该检查。