【发布时间】:2016-11-16 12:12:12
【问题描述】:
如何遍历div 中的所有label 项目。我的意思是有一堆未知数量的标签标签,它们又具有单选按钮。使用 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;
}
【问题讨论】:
标签: java html selenium selenium-webdriver webdriver