简体   繁体   English

复选框元素的Xpath通过按值定位其跟随的标签元素

[英]Xpath for checkbox element by locating it's following label element by value

I have been working for quite a while on this and still haven't found an answer specific to my problem in stackoverflow or from experimenting with the Xpath myself. 我已经为此工作了很长一段时间,但仍然没有找到针对我的stackoverflow问题或自己尝试Xpath的问题的答案。 I am quite inexperienced so I alpogise if this is a simple problem but I would really appreciate any help. 我没有经验,所以我很抱歉这是一个简单的问题,但是我会非常感谢您的帮助。

I am working with Selenium to test a web app that uses Wicket. 我正在与Selenium一起测试使用Wicket的Web应用程序。 I need the Xpath to the checkbox that correlates to the respective label. 我需要指向相应标签的复选框的Xpath。 This is because I need to be able to enter the value shown on the label and for it to find the relevant checkbox based on the label text such as "001", as the checkbox ids do not match the values. 这是因为我需要能够输入标签上显示的值,并使其能够基于标签文本(例如“ 001”)找到相关的复选框,因为复选框ID与值不匹配。

Mockup below shows the checkboxes and their corresponding labels; 下面的模型显示了复选框及其相应的标签;

小样

The corresponding HTML is show below; 相应的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>

Another problem I also face is that the Xpath must include the fact that it is inside the span shown in the html as there are 3 other groups of checkboxes on the page with the same values so it must be specific for each span for example: 我还面临的另一个问题是Xpath必须包含以下事实:它在html中显示的范围内,因为页面上还有其他3组复选框具有相同的值,因此它必须针对每个范围,例如:

id="excludeDepotCheckBox5"

I have tried the following Xpaths to no avail; 我尝试了以下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]"

Again I aplogise if it is a simple syntax/understanding problem but I would really appreciate any help. 再次抱歉,这是一个简单的语法/理解问题,但我将非常感谢您的帮助。

You can use the below xpaths: 您可以使用以下xpath:

1- For checking the checkbox related to label '001' : 1-用于选中与标签'001'相关的复选框

//span[@id='excludeDepotCheckBox5']/label[.='001']/preceding-sibling::input[1]

2- For checking the checkbox related to label '009' : 2-用于检查与标签“ 009”相关的复选框

//span[@id='excludeDepotCheckBox5']/label[.='009']/preceding-sibling::input[1]

NOTE: It will check for the 'input' element which is the first preceeding sibling of label element with exact innerHTML/text as '001' or '009' under a span element with id='excludeDepotCheckBox5'. 注意:它将检查“ input”元素,这是具有id ='excludeDepotCheckBox5'的span元素下具有完全innerHTML / text为'001'或'009'的label元素的第一个同级对象。

Since "preceding-sibling" is so error-prone (it will break as soon as the HTML structure changes a little bit), here's a more stable variant (wrapped for legibility): 由于“ 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM