Eric Furspan
Eric Furspan

Reputation: 761

Powershell: How to reference the nth element

I would like to grab the 3rd "td" element in "filterTable". How is this done in a .NET environment like Powershell ? I have tried numerous ways, like so:

$_.getElementsByTagName("td")[3] 
$_.getElementsByTagName("td[3]")
$_.getElementsByTagName("td:3") 
$_.getElementsByTagName("td{3}") 
$_.getElementsByTagName("td"){3} 

However none of these seem to work. Is there a way to do this? Thanks for any help. Here is some context of my code:

$textValues = @()
$textValues = $data.ParsedHtml.getElementById("filterTable") | foreach{
    $_.getElementsByTagName("td") | foreach{
        $_ | Select InnerText
    }
}

Upvotes: 1

Views: 1104

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174555

You can use the item() method on the element collection returned by getElementsByTagName().

Just supply an index (zero-based):

$filterTable = $data.ParsedHtml.getElementById("filterTable")
$3rdTD       = $filterTable.getElementsByTagName("td").item(2)

Alternatively, use Select-Object -Index:

$filterTable.getElementsByTagName("td") |Select-Object -Index 2

Upvotes: 5

Related Questions