0
<abc>
  <div>
    <input>
    <label>A
      <span>C</span>
    </label>
  </div>
  <div>
    <input>
    <label>AB
      <span>D</span>
    </label>
  </div>
</abc>

I need to select the <div> tag with search criteria of text in the <label> tag. Text inside <span> is dynamic and sometimes it is empty, so this should not be used as search criteria.

What I have so far tried is below, both could not return the answer:

//div[./label[.='A']]

//div[./label[text().='A']]
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Sidharth
  • 27
  • 7

3 Answers3

1

One way to do it with ancestor (first will select div with label "A", second will select div with label "AB") :

//label[./text()[normalize-space()="A"]]/ancestor::div
//label[./text()[normalize-space()="AB"]]/ancestor::div

With contains function :

//label[contains(./text(),"A") and string-length(normalize-space(./text()))=1]/ancestor::div
//label[contains(./text(),"AB")]/ancestor::div
E.Wiest
  • 5,425
  • 2
  • 7
  • 12
0

Use parent:

//label[text()='A']//parent::div

Also you can use text contains:

//label[contains(text(),'A')]//parent::div
frianH
  • 7,295
  • 6
  • 20
  • 45
0

To select the parent <div> element with respect to it's child <label> element's text using Selenium you can use the following based Locator Strategies:

  • Selecting <div> with child <label> text as AB

    //div[//label[contains(., 'AB')]]
    

Note: still doesn't supports so you have to use

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352