Skip to content Skip to sidebar Skip to footer

Get Element By Tag Name

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('Intern

Solution 1:

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

<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 100LoopUntil 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:

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

ForEach 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 100LoopUntil ie.ReadyState = 4'get the content of the iframeSet iframe = ie.document.getElementsByTagName("iframe")(0).contentWindow

'get the jobId input element inside the iframeForEach jobId In iframe.document.getElementsByName("jobId")
  MsgBox jobId.value, 64, "Job ID"Next

Post a Comment for "Get Element By Tag Name"