seleniumxpathcustom-selectors

XPATH Testing string value of child and parent has empty string value


I have html that I am searching against in a Selenium test where I want to find the child by its text only if the parent has no text:

Here's the HTML:

    <ul>
     <li>
      <div>
      "4"
       <span>630</span>
      </div>
    </li>
    <li>
     <div>

       <span>630</span>
     </div>
    </li>
    <li>
     <div>
     "6"
      <span>630</span>
     </div>
    </li>
   </ul>

Selecting for the text string '630' yields three results using "//span[.='630']" or "//span[text()='630']", but I only want that middle one with an empty parent. I've tried a whole lot of constructions that don't work "//span[.='630' and parent::div[.='']]" or "//span[text()='630' and parent::div[text='']]" and even "//div[text()='']/*[1] | //span[text()='630']" but these all result in either 3 results or none.

Note that while "//span[text()='630'] and .//div[contains(text(),'')]" detects the text, it returns true, and I need it to return the element so that I can click it.


Solution

  • //span[text()='630']/parent::div[not(text())]
    

    try this.