0

Given this html sample :

<div class="measure-tab"> --- i want to select this one
  <span class="color_title">someText</span>
</div>
<div class="measure-tab"> --- i dont want to select this one
  <span class="color_title">someText</span>
  <div>
      <span class="qwery">jokerText</span>
  </div>
</div>
<div class="measure-tab"> --- i want to select this one
  <span class="color_title">someText</span>
</div>

I want to select the div that has @class='measure-tab' which has under it a span that as a specific class and text = someText and a nested span that has a specific class and does not contain text = 'jokerText', all this in an XPATH

What i've tried is :

//div[contains(@class, 'measure-tab') and //span[@class="color_title" and (contains(text(),'someText')) and //span[@class="color_title" and not(contains(text(),'jokerText'))]]

But this dosen't seem to work. I also used This post as inspiration.

EDIT : Corrected bad description of what is the goal for this question

EDIT, made a new solution :

//div[contains(@class, 'measure-tab') and //span[contains(@class, 'color_title') and //span[not(contains(@class, 'qwery'))]]]

But this returns all the divs, instead of not matching it with --- i dont want to select this one

<span class="color_title">someText</span>
  <div>
      <span class="qwery">jokerText</span>
  </div>

I feel so close but yet so far, haha, it dosen't make sense for me why is it matching it with <span class="qwery">jokerText</span> when i wrote not contains there

michael
  • 7
  • 5
  • Does the bellow answers has solved your issue? if yes, please feel free to accept the correct answer with click icon `√` – frianH Aug 27 '20 at 12:48

2 Answers2

0

I believe this is what you are looking for-

MyDivs = driver.find_elements_by_xpath("//div[@class='measure-tab' and not(descendant::*[text() = 'jokerText' and @class = 'qwery'])]")

This will select all the div tag which does not have jokerText in it.

Swaroop Humane
  • 1,770
  • 1
  • 7
  • 17
  • Yeah, but won't this select the div that has `jokerText` also? – michael Aug 26 '20 at 14:48
  • Yeah, that's what I did but, it really gets messy with many other similar stuff, so I really wanna fiter it via xpath, thanks for the effort, if nobody else can help with this i might aswell pick ur answer – michael Aug 26 '20 at 15:14
  • I also edited the post with a new solution looking only by tag attributes, but it seems that not contains dosen't do what I hoped it did, which was to not get the div that has the attr `@class="qwery"` – michael Aug 26 '20 at 15:16
  • I have changed my code and tried that all your requirements met, Hope it works :(. – Swaroop Humane Aug 26 '20 at 15:25
0

You can query with not(following-sibling::div/span.....)

Try with following xpath:

//span[@class='color_title' and not(following-sibling::div/span[@class='qwery' and text()='jokerText'])]/parent::div[@class='measure-tab']
frianH
  • 7,295
  • 6
  • 20
  • 45