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

如何用Playwright单查询筛选无text-decoration:line-through的元素?

用Playwright通过CSS选择器排除带删除线的元素

可以直接通过单个CSS选择器实现需求,无需手动过滤,所有逻辑都包含在选择器中。

核心选择器

针对你的DOM结构,使用以下选择器即可选中所有不带text-decoration:line-through样式的子span元素:

span span:not([style*="text-decoration:line-through"]):not([style*="text-decoration: line-through"])

选择器说明

  • span span:定位外层span下的所有子span元素
  • :not([style*="text-decoration:line-through"]):排除style属性中包含text-decoration:line-through的元素
  • 额外添加:not([style*="text-decoration: line-through"])是为了兼容样式值前带空格的写法(比如text-decoration: line-through),提升通用性

Playwright代码示例(Java版本)

// 定位所有符合条件的元素
Locator targetElements = page.locator("span span:not([style*=\"text-decoration:line-through\"]):not([style*=\"text-decoration: line-through\"])");

// 如果需要获取第一个符合条件的元素,可直接链式调用
Locator firstTarget = targetElements.first();

这个选择器不受子span的顺序、数量影响,会自动筛选出所有不带删除线的元素,完全满足通用性要求。

内容的提问来源于stack exchange,提问作者user2173353

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 05:05:17