发布于2022-09-06 20:49 阅读(1250) 评论(0) 点赞(14) 收藏(1)
如何遍历label
. div
我的意思是有一堆未知数量的标签标签,它们又具有单选按钮。使用Selenium WebDriver。我需要找到选定的radio button
. 这里有两件事:
例如
<div class="controls">
<label class="radio inline">
<input type="radio" value="0" name="PlaceOfResidence"/>
Urban
</label>
<label class="radio inline">
<input type="radio" value="1" name="PlaceOfResidence"/>
Suburb
</label>
<label class="radio inline">
<input type="radio" value="2" name="PlaceOfResidence"/>
Rural
</label>
<label class="radio inline">
<input type="radio" value="3" name="PlaceOfResidence"/>
Not Available
</label>
</div>
这是我尝试过的
private String isRadioButtonSelected2(String name){
List<WebElement> webEl = this.getWrappedDriver().findElements(By.xpath("//input[@type='radio' and @name = '"+name+"']/parent::label")); //and @value='"+value+"']"));
String selectedValue = "";
for(WebElement element: webEl){
Boolean selectedRadio = element.isSelected();
if(selectedRadio){
selectedValue =this.getWrappedDriver().findElement(By.xpath("//input[@type='radio' and @name = '"+name+"']/parent::label")).getText();
log("&&&&&&&&&&"+selectedValue);
}else{
return null;
}
}
return selectedValue;
}
而不是使用xpath
来查找所有单选按钮,您只需简单地使用它就可以找到它,By.name
这比xpath
. 尝试如下: -
List<WebElement> radioButtons = this.getWrappedDriver().findElements(By.name("PlaceOfResidence"));
int size = radioButtons.size();
// This is the count of total radio button
for(WebElement radio : radioButtons)
{
If(radio.isSelected())
{
String selectedValue =radio.findElement(By.xpath("./parent::label")).getText();
}
}
希望能帮助到你...:)
作者:黑洞官方问答小能手
链接:http://www.qianduanheidong.com/blog/article/400019/25441e46340057b3aed3/
来源:前端黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 前端黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-3
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!