Search code examples
xpathhrefextract

Xpath expression returns null


I have the plenty of links like this:

<a href="/edit_flat/1674093.html"><b>Edit issue &gt;&gt;</b></a>

Trying to extract the href' content I use Xpath expression:

//a[contains(@href,'/edit_flat')]

but it returns me null. What am I doing wrong ?


Solution

  • //a[contains(@href,'/edit_flat')] selects a elements anywhere in the document tree that have an href attribute containing the '/edit_flat' string.

    These matching elements do have this very "href" attribute, but the XPath expression you are using returns "only" the a elements, if there are any.

    To actually return the matching elements' attribute's values, you need an extra step, with / and @href. So what you want is:

    //a[contains(@href,'/edit_flat')]/@href
    

    Suggestion:

    What you really want is probably to select links which href begin with the substring "/edit_flat", so it's safer to use:

    .//a[starts-with(@href,'/edit_flat')]/@href