pythonselenium-webdriverwebdriver

Selenium Xpath to select text between span


I am trying to select the HITT element shown below.

<span class="classname"> HITT </span>

So I tied the following Xpath expression:

//span[@class="classname" and text()="HITT"] 

Which does not work/find the element in question. The first part will work, i.e.

//span[@class="classname"] 

This finds all the elements where starting with . But when I add the "and text()" portion it doesn't work.

Please let me know what mistake I'm making here. Thanks! enter image description here


Solution

  • <span class="classname"> HITT </span>
    

    If you notice, HITT has a trailing and leading whitespace. Hence text()="HITT" won't work.

    Try this:

    //span[@class="classname" and contains(text(),"HITT")]