Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can't find text using XPATH contains and text()

Below is an example of the elements involved. I’m trying to write an XPATH that points to the list element that contains the string "This Is The Text You’re Looking For" but

li//[contains(text(), 'This Is The Text You're Looking For')]

doesn’t work. Neither does using the wildcard character ‘*’ in place of ‘li’ in the XPATH.

<li class = "yadda yadda yadda" tabindex="-1" role="option" aria-selected="false" data-value="3">
    <svg xmlns="https://something" viewBox="a b c d" class="yadda yadda yadda" focusable="false" aria-hidden="true"</svg>
     "&nbsp;"
     "This Is The Text You're Looking For"
     <span class="yadda yadda yadda"></span>
</li>

Why isn’t my XPATH working, and what do I need to do to point to the list element in my example by looking for that string?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

First, it seems you have namespace declarations somewhere in the document. You have have to either take them into account or use the element’s local-name().

Also, take a look at the difference between . and text() in xpath.

Then try this and see if it works:

//*[local-name()="li"][contains(., "This Is The Text You're Looking For")]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading