2

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>

And my VBS is:

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

But the output does not show the value "24", it is just echoing

value is=

How can I get that value?

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
B K
  • 39
  • 7

3 Answers3

2

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).

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'

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.

That way the next error:

set val =ie.document.getElementById("test").item(1).value
==>
... runtime error: Object doesn't support this property or method: 'ie.document.getElementById(...).item'

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].

The next version

set val =ie.document.getElementById("test").value

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'

A google query like "html dom div text" will point you to "innerText" and its features: 1 or 2

At last:

set elm =ie.document.getElementById("test")  ' at least a decent assignment
val = elm.innerText

success!

cscript 23971918.vbs
value is= 24
Community
  • 1
  • 1
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96
1

It looks like you need change getElementsById with getElementById.

Dalorzo
  • 19,834
  • 7
  • 55
  • 102
0

Someone mentioned you should change the getElementsById to getElementById (no 's'), but I also think you should lose the .item(1) in this line: set val =ie.document.getElementsById("test").item(1).value

If I'm remembering correctly, using .item(#) would be appropriate if your object was a collection, like something returned by using .getElementsByTagName but .getElementById should only return one item.

Hope that helps!

cilla
  • 46
  • 4