Html document.getElementByID-检查是否找到元素
下面是一个示例代码,用于打开internet explorer窗口,导航到google,并通过其唯一id获取页面上的某些元素:Html document.getElementByID-检查是否找到元素,html,dom,scripting,vbscript,Html,Dom,Scripting,Vbscript,下面是一个示例代码,用于打开internet explorer窗口,导航到google,并通过其唯一id获取页面上的某些元素: set ie = CreateObject("InternetExplorer.Application") ie.navigate("www.google.com") ie.visible = true while ie.readystate <> 4 wscript.sleep 100 WEnd set some_object = ie.do
set ie = CreateObject("InternetExplorer.Application")
ie.navigate("www.google.com")
ie.visible = true
while ie.readystate <> 4
wscript.sleep 100
WEnd
set some_object = ie.document.getelementbyid("xjsc")
MsgBox some_object.tagname, 0
因为它给了我以下错误:
ie.vbs(12, 1) Microsoft VBScript runtime error: Object required: 'some_object'
那么,检查元素是否被找到的最佳实践是什么
if isObject(some_object) then
如果变量包含一个对象,那么它被找到了
[更新]
经过一些测试和注释后,您需要使用isNothing
方法
由于您正在设置一个对象,它将始终是一个对象变量类型,但如果找不到它,它将设置为nothing
if isNothing(some_object) then
我已经用您的示例代码检查了上述内容,并且它按照预期工作
用vbscript的术语来说就是
if some_object is nothing then
是的,但在这两种情况下,它都会为我重新运行
true
,即使什么也没有找到@现在就到这里,查看isNull
(甚至isEmpty
)。根据规范,如果找不到任何元素,它将返回null。我做了,不管元素是否被找到,它们似乎都输出false。事情变得越来越奇怪<代码>D:\work\\u tmp\ie.vbs(12,1)Microsoft VBScript运行时错误:类型不匹配:“isnothing”顺便说一下,isnothing是VB6函数,不是吗?我还没有在VBScript引用中找到它。@现在到这里来,你是对的。。我是用vb来思考的。。vbscript等价物是,如果某个对象什么都不是,则
if some_object is nothing then