Get element by tag name

一个人想着一个人 提交于 2021-01-28 21:31:45

问题


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 ) enter image description here

Please help Thanks !!


回答1:


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


来源:https://stackoverflow.com/questions/22286360/get-element-by-tag-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!