Hui
Hui

Reputation: 14457

XPath/HTMLAgilityPack problems

I want to get player lists from here:

http://www.basketball-reference.com/boxscores/201105090BOS.html

To do that for the first table, I use the following:

HtmlNode reboundsNode = doc.DocumentNode.SelectSingleNode("//table[@class='sortable stats_table']/tbody[1]");
    foreach(HtmlNode node in reboundsNode.SelectNodes("tr"))
    {
        // Get the 'td's.
    }

I had to split it into two lines, because "//table[@class='sortable stats_table']/tbody[1]/tr" selected trs from all of the table bodies instead of just the first one. Does anyone know why?

I also have problems when getting the data from the second table (actually table number 3 in the source since there are tables 2 and 4 that are invisible in the default view). When I select "//table[@class='sortable stats_table']", it shows that there are four tables, but when I do "//table[@class='sortable stats_table'][3]", it finds nothing (I get an unbound object exception when I try to use the result. Why is that?

Upvotes: 0

Views: 748

Answers (1)

SergeS
SergeS

Reputation: 11779

because XPath [] is not a number of table body, but condition , so 1 mean always true - try this - it will select from first tbody

 //table[@class='sortable stats_table']/tbody[position() = 1]/tr

Second question

 //table[@class='sortable stats_table'][3]

This is invalid xpath - correct way to write this is

 //table[@class='sortable stats_table' and position() = 3]

Note: position starts from 1 not from 0 and ends at elements count.

Upvotes: 2

Related Questions