从网页获取元素的 VBS 无法正常工作 [英] VBS to get an Element from a web page is not working properly
问题描述
我想在我的 VBS 中获取值 '24',该值在 ID 为 'test' 的 div 中设置.我的 HTML 是:
I want to get the value '24' in my VBS, which is set in the div with id 'test'. My HTML is:
<html><body>
Welcome <br /> Value: = <div id="test">24</div>
<br> Name: <p id="name">Someone</p><br>
</body></html>
我的 VBS 是:
on error resume next
set ie=createobject("internetExplorer.Application")
ie.navigate "http://localhost/h/getid.html"
ie.visible = false
wscript.sleep 2000
dim val
set val =ie.document.getElementsById("test").item(1).value
wscript.echo "value is= "& val
但是输出没有显示值24",它只是在回显
But the output does not show the value "24", it is just echoing
值为=
我怎样才能获得那个价值?
How can I get that value?
推荐答案
您不应该在此处提出与具有活动On Error"的脚本有关的问题Resume Next".那是在浪费大家的时间.不要隐藏错误/注意错误消息,您可以自行解决问题(大多数情况下).
You should not ask a question here that concerns a script with an active "On Error Resume Next". That is a waste of everybody's time. By not hiding errors/Pay attention to error messages, you can solve the problem(s) on your own (most of the time).
删除/停用 OERN 即可获得
Delete/Deactive the OERN and you get
set val =ie.document.getElementsById("test").item(1).value
==>
... runtime error: Object doesn't support this property or method: 'ie.document.getElementsById'
即使你不认识错字,谷歌搜索html dom getelementsbyid"会将您重新路由到Ergebnisse für [即结果] html dom getelementbyid".按照第一个链接之一(eg) 以更新您对该方法的了解.
Even if you don't recognize the typo, a google search for "html dom getelementsbyid" will re-route you to "Ergebnisse für [i.e. results for] html dom getelementbyid". Follow one of the first links (e.g.) to refresh you knowledge about that method.
这样下一个错误:
set val =ie.document.getElementById("test").item(1).value
==>
... runtime error: Object doesn't support this property or method: 'ie.document.getElementById(...).item'
不会让你感到惊讶.元素不是项目/元素的集合.[BTW:你不应该在这里发布答案,至少不需要基本测试].
won't surprise you. An element isn't a collection of items/elements. [BTW: You shouldn't post answers here without at least basic tests].
下一个版本
set val =ie.document.getElementById("test").value
应该引发红色警报:带有 Set 的赋值,但是想要成为一个正确的值对象的属性.那是大错特错.所以试试:
should raise a red alert: An assignment with Set, but a right value that wants to be a property of an object. That is blantantly wrong. So try:
set elm =ie.document.getElementById("test") ' at least a decent assignment
val = elm.value
==>
... runtime error: Object doesn't support this property or method: 'elm.value'
像html dom div text"这样的谷歌查询会指向innerText"及其功能:1 或 2
A google query like "html dom div text" will point you to "innerText" and its features: 1 or 2
最后:
set elm =ie.document.getElementById("test") ' at least a decent assignment
val = elm.innerText
成功!
cscript 23971918.vbs
value is= 24
这篇关于从网页获取元素的 VBS 无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!