Thirumalai Parthasarathi
Thirumalai Parthasarathi

Reputation: 4671

why is this vbscript not fetching any value?

i am trying to write a vbscript that extracts the value from src attribute of an <img> tag that has a class attribute as this cs-poster-big

here is the code i have tried so far,

'Initializing object with Internet Explorer Application
set IE = WScript.CreateObject("InternetExplorer.Application", "IE_")

'setting properties of Internet Explorer to the newly create object
with IE
  .Visible = 0
  .navigate "http://www.roku.com/channels/#!details/12" 'INSERT WEBPAGE HERE
end with

'waiting for IE to load the page
'tried using IE.busy or IE.readyState <> 4 also
while IE.busy
  wScript.sleep 500
wend

wScript.sleep 500
'getting all image tags from the webpage
Set imgTags = IE.document.getElementsByTagName("IMG")

'iterating through the image tags to find the one with the class name specified
 For Each imgTag In imgTags
      'tried imgTag.className also
  If imgTag.getAttribute("class") = "cs-poster-big" Then MsgBox "src is " & imgTag.src

next

IE.quit
set IE= Nothing

MsgBox "End of script"

and it is not displaying any value but you can view the source of the page here and you can see it has an <img> tag with class cs-poster-big

i don't understand why it is not displaying in my script

Upvotes: 0

Views: 874

Answers (1)

MC ND
MC ND

Reputation: 70923

Do While IE.Busy Or IE.ReadyState <> 4 
    WScript.Sleep 500
Loop

Wait until page is completely loaded.

EDIT - While this works in IE10, IE8 fails to locate the image. Not tested in other versions.

In this case, try to change your url to

http://www.roku.com/channels?_escaped_fragment_=details/12/netflix#!details/12/netflix

to avoid problems with the dynamic generate content.

Also, in IE8, code needs to be changed to get class name of the image. It should be

Do While IE.Busy Or IE.ReadyState <> 4 
    WScript.Sleep 100
Loop

Set imgTags = IE.document.getElementsByTagName("IMG")

 For Each imgTag In imgTags
    imgClass = imgtag.getAttribute("class")
    If IsNull( imgClass ) Then 
        imgClass = imgTag.className
    End If
    If imgclass = "cs-poster-big" Then 
        MsgBox "src is " & imgTag.src
    End If
Next

But not a solution, just a workaround.

Upvotes: 1

Related Questions