如何select div 标签与标签标签文本节点的值

How to select the div tag with the value of text node of label tag

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

我需要 select <div> 标签和 <label> 标签中的文本搜索条件。 <span> 中的文本是动态的,有时是空的,因此不应将其用作搜索条件。

目前我试过的是下面的,都不能return答案:

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

//div[./label[text().='A']]

使用parent:

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

您也可以使用文本包含:

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

到 select 父 <div> 元素相对于其子 <label> 元素的文本使用 you can use the following based :

  • 选择 <div><label> 文本为 AB

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

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

使用 ancestor 的一种方法(首先将 select div 与标签 "A",第二个将 select div 与标签 "AB") :

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

具有contains功能:

//label[contains(./text(),"A") and string-length(normalize-space(./text()))=1]/ancestor::div
//label[contains(./text(),"AB")]/ancestor::div