![](/img/trans.png)
[英]How to check if the radio button is selected or not in Selenium WebDriver?
[英]Find the selected radio button in a list of <label> using selenium webdriver
如何遍歷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;
}
無需使用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();
}
}
希望能幫助到你...:)
//這將給出無線電元素的數量
List<WebElement> radioButtons = driver.findElements(By.xpath("//input[type=radio]")); int size = = radioButtons.size();
//迭代上述元素,並使用isSelected()方法標識選定的單選元素
希望這澄清
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.