简体   繁体   English

XPATH:排除具有特定子元素的元素

[英]XPATH: exclude elements which which has a specific child

<ul>
  <li><a></li>
  <li><a></li>
  <li>
    <div>
      <a>
    </div>
  </li>
</ul>

How can I select only first 2 li items and exclude li that contains div inside?如何只选择前 2 个 li 项并排除其中包含 div 的li

Considering ul is set as parent, I've tried考虑到ul被设置为父母,我试过了

./li[not(contains(descendant::div]

But obviously that didn't work但显然那没有用

Remove the contains and use // instead of ./删除contains并使用//而不是./

//li[not(descendant::div)]

Xpath that searches for li tags that has not div child:搜索没有div child 的li标签的 Xpath:

//li[not(descendant::div)]

another option:另外一个选择:

//li[not(.//div)]

To exclude some elements from selection - use not() method like locator[not(condition)]要从选择中排除某些元素 - 使用not()方法,如locator[not(condition)]

Some more complex example on this table https://www.w3schools.com/css/css_table.asp : here is locator that gets row that has not elements inside with text that starts with "C"此表上的一些更复杂的示例https://www.w3schools.com/css/css_table.asp :这里是定位器,它获取没有内部元素且文本以“C”开头的行

//table[@id='customers']//tr[not(.//*[starts-with(text(),'C')])]

explanation:解释:

//table[@id='customers']//tr - get all rows //table[@id='customers']//tr - 获取所有行

[not(.//*[...])] - not containing element with specific attribute [not(.//*[...])] - 不包含具有特定属性的元素

[starts-with(text(),'C')] - and that the described attribute [starts-with(text(),'C')] - 以及所描述的属性

//li[not(contains(div,' '))]

This works for me.这对我有用。

your code:你的代码:

./li[not(contains(descendant::div] ./li[不(包含(后代::div]

is missing some ')', should have been:缺少一些')',应该是:

./li[not(contains(descendant::div))]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM