1

I need to get the span element by matching its entire text which has a line break.

Below is sample HTML:

<div class="class1">
    <a>
        <span>fourth<br>team</span>
    </a>
</div>

I know we can use //span[contains(text(),'fourth')], but is there any way to match the entire text?

1

If you insist on using XPath, you could use normalize-space() function which would take care of spaces and newlines for you:

//a[normalize-space(span) = 'fourthteam']

Or, if you'd like to locate the span element instead:

//span[normalize-space(.) = 'fourthteam']
  • Thanks @alecxe...Unfortunately could not upvote due to less than 15 rep... – Sandesh Sawant Feb 24 at 13:54
  • 1
    Then let me do it for you! – Vishal Aggarwal Apr 11 at 0:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.