我需要通過基于 CSS 屬性的過濾來使用 Playwright 選擇合適的元素。在我的特殊情況下,我需要過濾掉具有該text-decoration:line-through屬性的元素并關注其余元素。
這是帶有元素的 DOM:

<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 代碼,我知道……雖然我對此無能為力……)
在這種情況下,我需要選擇 2nd span,它缺少洗掉線樣式,但span無法預料元素的順序和數量。
有沒有辦法用一個ilocator.Locator(selector);查詢來做到這一點?我需要用一個查詢來完成它,因為我創建的代碼需要是通用的,而不是假設事情并進行“手動”過濾。一切都需要在選擇器中。
uj5u.com熱心網友回復:
您可以使用包含運算子“*=”。它應該類似于:
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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/537402.html
上一篇:選擇所有日語文本,但不使用羅馬化
