在 VBA 中,如果我使用 getElementByID("id_name") 并且 id 不存在,则该函数不会返回任何内容,而不是 null。这让我不知道 DOM 是否尚未渲染该元素,或者该元素是否确实不存在。规范似乎要求返回 NULL,而 NULL 不等于 Nothing。所以我的问题是这个 DOM 函数是否返回 NULL、Nothing,或者它是否依赖于我缺少的东西?谢谢
狙击
If Not IsNull(p_IE.Document.getElementById(MAIN_SR_CONTAINER)) Then
' If I do not receive NULL I want to assume that I can grab the element.
' Still, I verify that the element is not Nothing
' problem is that NULL <> Nothing so if the element does not exist my code loops for eternity
' I do look at the readystate of the p_IE object and wait till it = 4
' But the elements may be being created by embedded javascript on the fly
Set elMainSRContainer = p_IE.Document.getElementById(MAIN_SR_CONTAINER)
Do While elMainSRContainer Is Nothing
Set elMainSRContainer = p_IE.Document.getElementById(MAIN_SR_CONTAINER)
Loop
:
:
Else
' bail
End If
getElementById 的 MSDN 文档 表示该方法返回值是 IHTMLElement 类型。这将是 VBA 中的
Object
类型。文档继续说该方法
返回具有指定 ID 的第一个对象,如果没有匹配则返回 null。
我的猜测是,因为在 VBA 中,
Objects
无法容纳 Null
,因此 Null
被解释为 Nothing
。
我会尝试改变
If Not IsNull(p_IE.Document.getElementById(MAIN_SR_CONTAINER)) Then
到
If Not (p_IE.Document.getElementById(MAIN_SR_CONTAINER) Is Nothing Then
一切都好。您只需要将 ID 名称放在引号“”中即可
那么
If Not IsNull(p_IE.Document.getElementById("MAIN_SR_CONTAINER")) Then
尝试一下。