我可以根據特定值選擇一行,然后獲取所需的列。示例:單擊學士型別學生的操作按鈕:
//td[text()='Bachelor']/following-sibling::td[@class='action']
正確選擇:

我要基于否定選擇的 HTML :單擊所有非 Master 型別的學生的操作按鈕
//td[not(text()='Master')]/following-sibling::td[@class='action']
但錯誤地選擇了兩者(應該只選擇學生的行):

使用 chrome XPath 決議器 (f12) 進行測驗,以便稍后在 Selenium 中使用 XPath 查詢。
在我所做的研究中找不到任何類似的例子或深入的解釋。
uj5u.com熱心網友回復:
原因
//td[not(text()='Master')]/following-sibling::td[@class='action']
正如您所觀察到的那樣,“也選擇主行中的[td元素]”是
//td[not(text()='Master')]
td選擇不具有text()字串值為 的子節點的所有元素'Master'。'Master' td重要的是,這包括您試圖排除的所有前任兄弟姐妹,問題是他們也有以下td兄弟姐妹。因此,您并沒有真正排除要排除的行。
要僅選擇緊接在其字串值為not的 a 之前的操作td元素,請使用以下 XPath:td 'Master'
//td[@class='action'][preceding-sibling::*[1][self::td][not(.='Master')]]
或者,您可以簡單地測驗該行而不是直接兄弟:
//tr[not(td='Master')]/td[@class='action']
請注意,前一個 XPath 對td定位敏感,而后一個 XPath 不會。
也可以看看
- 如何正確使用 XPath 前同級?
uj5u.com熱心網友回復:
只需使用:
//td[5][not(. = 'Master])]/following-sibling::td[@class = 'action']
或者簡單地說:
//tr[not(td[5] = 'Master')]/td[@class = 'action']
我認為使用它是危險的,//tr[not(td = 'Master')]因為很容易有姓氏的人'Master'
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/490154.html
