[英]Xpath for checkbox element by locating it's following label element by value
我已經為此工作了很長一段時間,但仍然沒有找到針對我的stackoverflow問題或自己嘗試Xpath的問題的答案。 我沒有經驗,所以我很抱歉這是一個簡單的問題,但是我會非常感謝您的幫助。
我正在與Selenium一起測試使用Wicket的Web應用程序。 我需要指向相應標簽的復選框的Xpath。 這是因為我需要能夠輸入標簽上顯示的值,並使其能夠基於標簽文本(例如“ 001”)找到相關的復選框,因為復選框ID與值不匹配。
下面的模型顯示了復選框及其相應的標簽;
相應的HTML如下所示;
<span wicket:id="excludeDepotCheckBox" id="excludeDepotCheckBox5">
<input name="adminPreferenceSection:excludeDepotCheckBox" type="checkbox" value="0" id="excludeDepotCheckBox5-adminPreferenceSection:excludeDepotCheckBox_0">
<label for="excludeDepotCheckBox5-adminPreferenceSection:excludeDepotCheckBox_0">001</label>
<br>
<input name="adminPreferenceSection:excludeDepotCheckBox" type="checkbox" value="1" id="excludeDepotCheckBox5-adminPreferenceSection:excludeDepotCheckBox_1">
<label for="excludeDepotCheckBox5-adminPreferenceSection:excludeDepotCheckBox_1">009</label>
<br>
</span>
我還面臨的另一個問題是Xpath必須包含以下事實:它在html中顯示的范圍內,因為頁面上還有其他3組復選框具有相同的值,因此它必須針對每個范圍,例如:
id="excludeDepotCheckBox5"
我嘗試了以下Xpath都沒有用;
//span[@id='excludeDepotCheckBox5' and contains(., '009')]"
"//*[@id='excludeDepotCheckBox5' and ./label/text()='009']/preceding-sibling::*[@name='adminPreferenceSection:excludeDepotCheckBox']
//*[@id='excludeDepotCheckBox5' and ./label/text()='009']/preceding-sibling::input[1]"
再次抱歉,這是一個簡單的語法/理解問題,但我將非常感謝您的幫助。
您可以使用以下xpath:
1-用於選中與標簽'001'相關的復選框 :
//span[@id='excludeDepotCheckBox5']/label[.='001']/preceding-sibling::input[1]
2-用於檢查與標簽“ 009”相關的復選框 :
//span[@id='excludeDepotCheckBox5']/label[.='009']/preceding-sibling::input[1]
注意:它將檢查“ input”元素,這是具有id ='excludeDepotCheckBox5'的span元素下具有完全innerHTML / text為'001'或'009'的label元素的第一個同級對象。
由於“ preceding-sibling”非常容易出錯(只要HTML結構稍作更改,它就會中斷),這是一個更穩定的變體(為便於閱讀而包裝):
//span[@id = 'excludeDepotCheckBox5']//input[
@id = //span[@id = 'excludeDepotCheckBox5']//label[normalize-space() = '001']/@for
]
// * [@@ =='excludeDepotCheckBox5'] / label [contains(text(),'001')] //之前的兄弟姐妹:: input [1]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.