Search code examples
c#htmlxpathhref

Selecting the text portion of an <a href> HTML tag through XPATH


Consider the following:

<span>
    <li>
        <a href="javascript:__doPostBack('Send', 'ShowPackageDetails');"> Recall </a>
    </li>
</span>

I would like to grab the XPath to this element through the text value 'Recall'.

I know I can also select it by using:

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

But other elements on the page also contain the same string in their href attribute. I'd prefer to select based on the actual text instead of its attribute in this case because I know it's unique and I won't have to resort to index usage.

I've tried

//a[contains(@value, 'Recall')]

And other various combinations of text()='Recall', but I'm not getting any results at all.


Solution

  • @ is for selecting attributes. Use the following instead:

    //a[contains(., 'Recall')]