为什么这个vbscript没有获取任何值?

时间:2013-11-19 11:09:07

标签: dom vbscript

我正在尝试编写一个vbscript,它从src标记的<img>属性中提取具有class属性的cs-poster-big

属性

这是我到目前为止尝试的代码,

'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"

并且它没有显示任何值,但您可以查看页面来源here,并且您可以看到它带有<img> class cs-poster-big的广告代码p>

我不明白为什么它没有在我的剧本中显示

1 个答案:

答案 0 :(得分:1)

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

等到页面完全加载。

编辑 - 虽然这在IE10中有效,但IE8无法找到图像。未在其他版本中测试过。

在这种情况下,请尝试将您的网址更改为

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

避免动态生成内容的问题。

此外,在IE8中,需要更改代码以获取图像的类名。它应该是

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

但不是解决方案,只是一种解决方法。