0

在 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
4

2 回答 2

1

getElementById的MSDN 文档说方法返回值是 IHTMLElement 类型。这将是ObjectVBA 中的一种类型。该文档继续说该方法

返回具有指定 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
于 2014-11-05T05:09:53.607 回答
0

一切正常。您只需要将 ID 名称放在引号“”中

所以

If Not IsNull(p_IE.Document.getElementById("MAIN_SR_CONTAINER")) Then

试试看。

于 2021-07-30T20:05:46.610 回答