通过标签名称获取元素 [英] Get element by tag name

查看:59
本文介绍了通过标签名称获取元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法从 HTML 页面获取元素

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

暗淡的输入设置 IE = WScript.CreateObject("InternetExplorer.Application")IE.Visible = 1IE.Navigate "某个站点"

然后我想遍历站点(HTML 代码)

设置输入 = IE.Document.GetElementsByName("input")x = msgbox(输入)x = msgbox(inputs.Length)对于输入中的每个 Zx = msgbox("Item = "+Z,64, "输入")下一个

在第一个 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>

这是我之前的帖子 当您实际上打算使用 getElementsByTagName.前者根据元素的值返回元素 attribute name:

name="videoUrl" ...>

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

<input name="videoUrl" ...>

我注意到的另外两件事:

  • 您似乎没有等待 IE 完成页面加载(这可能解释了为什么您得到 Null 结果).Navigate 方法立即返回,因此您必须等待页面完成加载:

    做WScript.Sleep 100循环直到 IE.ReadyState = 4

  • inputs 包含一个 DispHTMLElementCollection,而不是一个字符串,所以尝试用 MsgBox 显示它会给你一个类型错误.集合的成员也是如此.如果要以字符串形式显示标签,请使用对象的 outerHtml 属性:

    对于每个 Z In 输入MsgBox "Item = " &Z.outerHtml, 64, "输入"下一个

Edit2: 只获取name 属性值为jobId 的元素的value 属性值> 你可以用这个:

对于IE.document.getElementsByName("jobId")中的每个jobIdWScript.Echo jobId.value下一个

Edit3:您尝试处理的页面包含一个 iframe(抱歉,我之前没有注意到).这就是阻止您的代码工作的原因.getElementsByNamegetElementsByTagName 等元素 getter 方法不能跨帧边界工作,因此您需要在 content 上运行这些方法> iframe.这应该有效:

Set ie = CreateObject("InternetExplorer.Application")ie.Visible = Trueie.Navigate "http://..."做WScript.Sleep 100循环直到 ie.ReadyState = 4'获取iframe的内容设置 iframe = ie.document.getElementsByTagName("iframe")(0).contentWindow'获取 iframe 中的 jobId 输入元素对于 iframe.document.getElementsByName("jobId") 中的每个 jobIdMsgBox jobId.value, 64, "作业 ID"下一个

I have a trouble getting an element from HTML page

what I do is I navigate to a site then I want to find an element called "jobId"

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

then I want to loop through the 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

on the first msgbox I get an error of unspecified error of NULL

the element is in an iFrame ( I don't know if its impacting some how )

when I use ViewSource this is the elements I want to use :

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

this is followed by my previous post Here

following your instructions I got to this point :

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

this is what I get ( and that's my best yet )

Please help Thanks !!

解决方案

You're using getElementsByName when you actually mean to use getElementsByTagName. The former returns elements based on the value of their attribute name:

<input name="videoUrl" ...>

whereas the latter returns elements based on the name of the tag:

<input name="videoUrl" ...>

Edit: Two other things I noticed:

  • You don't seem to wait for IE to finish loading the page (which might explain why you're getting Null results). The Navigate method returns immediately, so you have to wait for the page to finish loading:

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

  • inputs contains a DispHTMLElementCollection, not a string, so trying to display it with a MsgBox will give you a type error. Same goes for the members of the collection. If you want to display the tags in string form use the objects' outerHtml property:

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

Edit2: To get just the value of the attribute value of elements whose name attribute has the value jobId you could use this:

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

Edit3: The page you're trying to process contains an iframe (sorry, I failed to notice that earlier). This is what prevents your code from working. Element getter methods like getElementsByName or getElementsByTagName don't work across frame boundaries, so you need to run those methods on the content of the iframe. This should work:

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

这篇关于通过标签名称获取元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆