Angry Red Panda
Angry Red Panda

Reputation: 439

xPath expression is not finding text inside HTML

I have following XML:

<div> 
  <ul> 
    <li> 
      <a>
          Logout 1
     </a> 
    </li>  
    <li> 
      <a>
         Logout 2
     </a> 
    </li>  
    <li> 
      <a>
         Logout 3
     </a> 
    </li>  
    <li> 
       <a>
         Logout 4
     </a> 
    </li> 
  </ul> 
</div>

And I want to check if a a tag with the text Logout 4exists. I do this with the following expression:

/div/ul/li/a[text() = 'Logout 4']

Which doesnt seem to work, anyone can tell me what I am doing wrong?

I am testing my xPath on this site btw: http://www.xpathtester.com/xpath

Upvotes: 0

Views: 55

Answers (1)

har07
har07

Reputation: 89325

Your XPath didn't return any result because the inner text of the a element has leading and trailing spaces, which you can clear using normalize-space() :

/div/ul/li/a[normalize-space() = 'Logout 4']

demo

or, if you really want to evaluate only the first child text node within a :

/div/ul/li/a[normalize-space(text()) = 'Logout 4']

Upvotes: 2

Related Questions