【问题标题】:why is this vbscript not fetching any value?为什么这个 vbscript 没有获取任何值?
【发布时间】:2013-11-19 11:09:07
【问题描述】:

我正在尝试编写一个 vbscript,它从具有 class 属性的 <img> 标记的 src 属性中提取值作为此 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,你可以看到它有一个&lt;img&gt;标签和classcs-poster-big

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

【问题讨论】:

    标签: dom vbscript


    【解决方案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
    

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

    【讨论】:

    • +1,因为它工作了大约 3 次。但现在它在Set imgTags = IE.document.getElementsByTagName("IMG") 行给了我一个unspecified error 80004005。有什么建议吗?
    • 错误80004005通常是指某种权限问题。在我看来,IE 加载完毕,你的代码尝试访问页面的内容,但是一些超时回调已经开始执行,无法访问内容。使用错误控制包装对文档属性/方法的所有访问,并在出现错误时等待。你可以检查 document.readyState="complete"
    • 会试一试并告诉你.. :)
    • 效果很好,伙计...谢谢.. :) 但我可以知道为什么问题中给出的 URL 不起作用,而您给出的 URL 有效...?
    • 不是我的优点。页面中的内容是由一些 javascript 框架动态生成的。为了测试,我只是使用了禁用 javascript 的 broser。服务器完成了剩下的工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-26
    • 2019-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-06
    • 1970-01-01
    相关资源
    最近更新 更多