1

I'm following the accepted answer from this question: How to obtain href values from a div using xpath?

The answer claims the XPath query being used gets the href values from the links but this doesn't seem to be the case with my code. To clarify, this is the accepted answer:

//div[@class="widget-content"]//a/@href

My XPath is as follows:

//tr/td//a/@href

This only seems to select the links, rather than the href values.

enter image description here

So in this case, I then have to use a foreach loop to select the href values.

foreach(var link in links)
{
    var hrefValue= link.Attributes["href"].Value;
}

First question, why does this happen? Second question, is there a way to directly get the href values?

For further context. I'm trying to select all href values from inside a HTML table.

EDIT: Example of HTML I'm trying to parse: http://www.realtimetrains.co.uk/train/W90778/2019/04/29/advanced

Jake
  • 1,701
  • 3
  • 23
  • 44

1 Answers1

0

Use the following Xpath to fetch all elements and then use attribute method to get all href.

"//table[@class='table table-condensed train  realtime  advanced']//tr//td//span/a"

OR if you want to starts with tr then.

"//tr//td//span/a"
KunduK
  • 32,888
  • 5
  • 17
  • 41