LoveJavaTwo
LoveJavaTwo

Reputation: 225

How to check if a web element does not have another inner web element

I am using Selenium and Java to write a test, I need to check if a web element does not have another web element inside it, for example:

<div><span>bla bla</span></div>
<div><g></g></div>
<div><li></li></div>

I need to select the div tags which does not have <span> inside them, so do we have something like:

//div[not(.//span[text()='bla bla'])]

Please do not tell me how to do it in other ways as I already know, I am just wondering if I can do it in a way like the one above.

Upvotes: 0

Views: 425

Answers (1)

har07
har07

Reputation: 89285

Your XPath should've done what you wanted. It will select <div> element that doesn't contain, either direct child or nested, span element with text equals "bla bla". See the demo online : http://www.xpathtester.com/xpath/be01362aa9bb1385da6dcf819cf69376

input :

<div> 
  <div>
    <span>bla bla</span>
  </div>  
  <div>
    <g/>
  </div>  
  <div>
    <li/>
  </div> 
</div>

output :

<div>
    <g/>
  </div>

<div>
    <li/>
  </div>

Upvotes: 1

Related Questions