Get the position of a node in a list?

Viewed 63

I wonder how to find the position of one country in CoronaVirus update list https://www.worldometers.info/coronavirus by using Chrome console

I tried: $x("count(.//*[@id='main_table_countries_today']/tbody/tr/td[1][.='Vietnam']/preceding-sibling::*)")

enter image description here

Anyone could give me a light here. Thanks.

2 Answers

In your xpath you are applying preciding-siblings to the first td in a tr, but you should count siblings for tr as those are actually the rows.

This one worked for me:

$x("count(//table[@id='main_table_countries_today']//tr[td/text() = 'Vietnam']/preceding-sibling::*)")

Also in my opinion there is no need to do +1 at the end as the first row is irrelevant: the whole world. But you should know better, what you need.

enter image description here

Or here is yours fixed:

$x("count(.//table[@id='main_table_countries_today']/tbody/tr[td[1][.='Vietnam']]/preceding-sibling::*)")

To complete kishkin's answer, shortest form :

count((//td[.='Vietnam'])[1]/preceding::tr[@style=""])+1

Output : 93 (92+1)

And to exclude the Diamond Princess :

count((//td[.='Vietnam'])[1]/preceding::tr[@style=""][not(.//span)])+1

Output : 92 (91+1)

Related