1

从 HTML 页面获取元素时遇到问题

我要做的是导航到一个站点,然后我想找到一个名为“jobId”的元素

Dim inputs
Set IE = WScript.CreateObject("InternetExplorer.Application")
IE.Visible = 1
IE.Navigate "SOME SITE"

然后我想循环浏览网站(HTML CODE)

Set inputs = IE.Document.GetElementsByName("input")
x = msgbox(inputs)
x = msgbox(inputs.Length)

For each Z in inputs
    x = msgbox("Item =  "+Z,64, "input")
next

在第一个 msgbox 上,我收到一个未指定的 NULL 错误错误

该元素位于 iFrame 中(我不知道它是否会影响某些方面)

当我使用 ViewSource 时,这是我要使用的元素:

<input type="hidden" name="videoUrl" value="https://vid-uss2.sundaysky.com:443/v1/w38/flvstreamer?jobId=5850f72f-46e1-49e1-953b-2a9acdf6dd01&authToken=d88fc69cea0c48769c3cd42e8481cd47&videoId=default"></input>
<input type="hidden" name="videoSessionId" value=""></input>
<input type="hidden" name="displaySurvey" value="true"></input>
<input type="hidden" name="isFlashPlayer" value="true"></input>
<input type="hidden" name="posterLocation" value="http://d21o24qxwf7uku.cloudfront.net/customers/att/attlogoinvitation.png"></input>
<input type="hidden" name="jobId" value="5850f72f-46e1-49e1-953b-2a9acdf6dd01"></input>
<input type="hidden" name="sundayskyBaseUri" value="https://att.web.sundaysky.com/"></input>
<input type="hidden" name="oldBucketMode" value="false"></input>

接下来是我之前的帖子这里

按照您的指示,我到了这一点:

Dim jobId
Set IE = WScript.CreateObject("InternetExplorer.Application")
IE.Visible = 1
IE.Navigate "https://att.web.sundaysky.com/viewbill?bk=dNEdk01ykHC72rQil7D_iTzfjn7Qj4FN6fvJ_YE-ndY"
x = msgbox("Wait for page to load",64, "Job ID")
set jobId = IE.document.getElementsByName("jobId")
x = msgbox(jobId,64, "Job ID")

这就是我得到的(这是我最好的) 在此处输入图像描述

请帮忙谢谢!!

4

1 回答 1

1

当您getElementsByName实际上打算使用getElementsByTagName. 前者根据其属性 name的值返回元素:

<input name="videoUrl" ...>

而后者根据标签的名称返回元素:

<input name="videoUrl" ...>

编辑:我注意到的另外两件事:

  • 您似乎不需要等待 IE 完成页面加载(这可能解释了您获得Null结果的原因)。该Navigate方法立即返回,因此您必须等待页面完成加载:

    Do
      WScript.Sleep 100
    Loop Until IE.ReadyState = 4
    
  • inputs包含 a DispHTMLElementCollection,而不是字符串,因此尝试使用 a 显示它MsgBox会给您一个类型错误。集合的成员也是如此。如果要以字符串形式显示标签,请使用对象的outerHtml属性:

    For Each Z In inputs
      MsgBox "Item =  " & Z.outerHtml, 64, "input"
    Next
    

Edit2:要仅获取其属性具有该值value的元素的属性值,您可以使用以下命令:namejobId

For Each jobId In IE.document.getElementsByName("jobId")
  WScript.Echo jobId.value
Next

Edit3:您尝试处理的页面包含一个iframe(对不起,我之前没有注意到)。这就是阻止您的代码工作的原因。元素getter 方法喜欢getElementsByNamegetElementsByTagName不跨帧边界工作,因此您需要在. 这应该有效:iframe

Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True

ie.Navigate "http://..."

Do
  WScript.Sleep 100
Loop Until ie.ReadyState = 4

'get the content of the iframe
Set iframe = ie.document.getElementsByTagName("iframe")(0).contentWindow

'get the jobId input element inside the iframe
For Each jobId In iframe.document.getElementsByName("jobId")
  MsgBox jobId.value, 64, "Job ID"
Next
于 2014-03-09T18:47:41.030 回答