1

I am retrieving an array of elements from a webpage via .getElementsByTagName(). I am then looping through the array and printing out the elements' titles:

$links = $ie.document.GetElementsByTagName("a")
Foreach($link in $links)
{
    $link.title
}

This works. However a for loop better suits my needs, and this only seems to print out blank lines:

$links = $ie.document.GetElementsByTagName("a")
for($i=0; $i -lt $links.length; $i++)
{
    $links[$i].title
}

Why does the second loop not print the title?

Gerald
  • 521
  • 1
  • 6
  • 16
  • I'd use PowerGui to inspect the object $links in both cases and make sure they are of the same type, and PowerShell isn't casting them somehow... – Austin T French Jun 29 '16 at 22:41
  • 1
    Possible confirmation, duplicate: http://stackoverflow.com/questions/17625309/use-getelementsbyclassname-in-a-script – Austin T French Jun 29 '16 at 22:47

2 Answers2

1

The 6 methods below all print all link titles, except Select-Object also prints an empty line when the title attribute is missing:

$requestUri = "http://google.com"

$ie = New-Object -ComObject "InternetExplorer.Application"
$ie.Navigate($requestUri)
while($ie.Busy) { Start-Sleep -Milliseconds 250 }
$links = $ie.Document.getElementsByTagName("a")

Write-Warning "ForEach-Object:"
$links | ForEach-Object { $_.title }

Write-Warning "Select-Object:"
$links | Select-Object title

Write-Warning "foreach:"
foreach($link in $links) { $link.title }

Write-Warning "for (from 0):"
for($i = 0; $i -lt $links.Length; $i++) {
    $links[$i].title
}

Write-Warning "for (from 1):"
for($i = 1; $i -le $links.Length; $i++) {
    $links[$i].title
}

Write-Warning "while:"
$i = 0
while($links[$i]) {
    $links[$i].title
    $i++
}

$ie.Quit()

Can you please try to run this on your system?

sodawillow
  • 12,497
  • 4
  • 34
  • 44
  • 1
    Right, I have revised my answer. – sodawillow Jun 30 '16 at 08:07
  • 1
    I have PowerShell v5 installed, and I guess you have v2 or v3. I'm running tests on several versions atm. – sodawillow Jun 30 '16 at 12:43
  • That is correct, I have v3. v2 is even much more lacking in terms of the DOM methods it supports, I believe. – Gerald Jun 30 '16 at 13:52
  • 1
    Right. So either you upgrade your version of PowerShell, or you use one of the v3-compatible methods. – sodawillow Jun 30 '16 at 13:59
  • Yes, the Select-Object, with a where clause, method you mentioned is actually preferable to me rather than explicitly looping through all the elements. Thank you! – Gerald Jun 30 '16 at 15:49
-1

I would guess that $links = $ie.document.GetElementByTagName("a") and and $links = $varDiv.GetElementsByTagName("a") are not Equivalent in that they are not providing you with the same object type.

sodawillow
  • 12,497
  • 4
  • 34
  • 44
reemul
  • 95
  • 1
  • 10
  • Whoops. They are both pulling from a subset of elements within a div, but I meant to edit them both here as pulling from the entire document for the sake of the brevity of the question. Edited. – Gerald Jun 29 '16 at 22:17