css - Playwright - 根据 CSS 属性选择元素

标签 css playwright playwright-dotnet

我需要通过基于 CSS 属性的过滤来使用 Playwright 选择适当的元素。在我的特定情况下,我需要过滤掉具有 text-decoration:line-through 属性的元素,并专注于其余元素。

这是包含元素的 DOM:

DOM elements

<span>
    <span style="line-height:1.15 !important;display:initial;text-decoration:line-through;display:block;">0.42</span>
    <span style="line-height:1.15 !important;display:initial;font-weight:bold;">0.29</span>
</span>

(糟糕的 HTML 代码,我知道...对此我无能为力...)

在本例中,我需要选择第二个 span,它缺少删除线样式,但无法预期 span 元素的顺序和数量.

有没有办法通过单个ilocator.Locator(selector);查询来做到这一点? 我需要使用单个查询来完成此操作,因为我创建的代码需要是通用的,而不是假设事物并进行“手动”过滤。一切都需要在选择器中。

最佳答案

您可以使用包含运算符“*=”。它应该类似于:

page.locator('span[style*="text-decoration:line-through"]')

CSS 演示:

span {
  color: orange;
}

/* target span with line-through */
span[style*="text-decoration:line-through"],
/* handle white space */
span[style*="text-decoration: line-through"] {
  color: blue;
}

/* target span without line-through */
span:not([style*="text-decoration:line-through"]),
/* handle white space */
span:not([style*="text-decoration: line-through"]) {
  color: red;
}
<span>
  <span style="stuff">a</span>
  <span style="stuff; text-decoration:line-through; foo">b</span>
</span>

关于css - Playwright - 根据 CSS 属性选择元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74512790/

相关文章:

javascript - 在剧作家的第一个浏览器实例之后保持登录 session

python - 使用 Playwright for Python,如何读取输入框的内容

javascript - 使用剧作家导航后元素不可见

c# - 剧作家 "Element is not attached to the DOM"

html - 更改警报错误位置

javascript - 样式 ="display: none"取代 css currentcamera wordpress athena 主题

html - -webkit-text-size-adjust 用于桌面浏览器

html - 我希望我的 div 的边界完全重叠,但是当我将它们与 flex 对齐时会变得奇怪