You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于现有XPath过滤掉class为a-row a-spacing-micro的子div元素?

Solution for Filtering Divs Without Specific Child Elements

Got it, let's sort out this XPath filtering task for you! You want to adjust your original expression to target only those parent divs that don't have a child div with the class a-row a-spacing-micro. Here are two solid ways to do it:

Approach 1: Exact class match (direct children)

Use the not() function to exclude elements that have a direct child div with the exact class string:

//div[@class='a-section a-spacing-none a-spacing-top-small' and not(div[@class='a-row a-spacing-micro'])]

Breakdown:

  • Your original selector //div[@class='a-section a-spacing-none a-spacing-top-small'] grabs all the base elements you care about.
  • The and not(div[@class='a-row a-spacing-micro']) clause adds a filter: only keep elements that do not have a direct child div with exactly that class.

Approach 2: Flexible class match (handles extra classes)

If the child div might have additional classes mixed in (like a-row a-spacing-micro some-other-class), use contains() to check for the presence of your target class substring:

//div[@class='a-section a-spacing-none a-spacing-top-small' and not(div[contains(@class, 'a-row a-spacing-micro')])]

Breakdown:

  • contains(@class, 'a-row a-spacing-micro') checks if the child's class attribute includes your target class string, which works even if there are other classes present.

Bonus: Check all descendants (not just direct children)

If you need to exclude elements that have the target div anywhere inside them (not just as a direct child), use .//div to look through all descendants:

//div[@class='a-section a-spacing-none a-spacing-top-small' and not(.//div[@class='a-row a-spacing-micro'])]

内容的提问来源于stack exchange,提问作者Kevin.a

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:10:12