0

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

1
  • 1
    If you don't need exact comparison, you can also use contains(.,'Logout 4'), like this: /div/ul/li/a[contains(.,'Logout 4')] Commented Apr 25, 2017 at 7:13

1 Answer 1

2

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']
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.