如何检查VBA中是否存在元素 [英] How to check if element is not exist in VBA

查看:493
本文介绍了如何检查VBA中是否存在元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。



我正在尝试从网站上删除数据。



在某些情况下如果数据已被删除,则网站会发生变化。



所以我使用

Hi All.

I am trying to scrapping data from website.

In some case if the data have been delete the website is change.

So I Use

appIE.document.getElementsByClassName("alert-block")





但我不知道如何识别班级



But I don't know how to identify if the class

("alert-block")

退出与否。



我在这里阅读了这篇文章





[ ^ ] [ ^ ]



并且他们说



exits or not.

I have read the artical here


[^][^]

And they say that the

If MyObject Is Nothing Then  ' <--- This check always returns False





我尝试过:





What I have tried:

If appIE.document.getElementsByClassName("alert-block") Is Nothing Then



'做某事1。

其他

'在这里做点什么。

endif


'Do something1.
else
'do something2 here.
endif

推荐答案

getElementsByClassName 返回一个集合。如果找不到任何元素,则返回没有项目的集合。但它仍然是一个集合对象,因此检查 Nothing 将始终失败。而是使用 Count 属性检查集合是否为空:

getElementsByClassName returns a collection. If no element can be found a collection with no items is returned. But it is still a collection object so that checking for Nothing will always fail. Instead check if the collection is empty or not by using the Count property:
Set element = appIE.document.getElementsByClassName("alert-block")
If element.Count > 0
' Element exists and is not empty
Endif


这篇关于如何检查VBA中是否存在元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆