[英]Find the selected radio button in a list of <label> using selenium webdriver
How to loop through all the label
items in a div
. 如何遍历
div
所有label
项。 I mean there are a bunch of unknown number of label tags which inturn have radio button. 我的意思是有一堆未知数量的标签标签,这些标签标签依次具有单选按钮。 Using Selenium WebDriver .
使用Selenium WebDriver 。 I need to find the selected
radio button
. 我需要找到选中的
radio button
。 There are two things here: 这里有两件事:
For example 例如
<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>
Here is what I've tried 这是我尝试过的
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;
}
Instead of using xpath
to find all radio buttons you can find it just simply using By.name
which is much faster than xpath
. 无需使用
xpath
查找所有单选按钮,只需使用By.name
即可找到它,该方法比xpath
快得多。 Try as below :- 尝试如下:-
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();
}
}
Hope it helps...:) 希望能帮助到你...:)
//This will give the number of radio elements //这将给出无线电元素的数量
List<WebElement> radioButtons = driver.findElements(By.xpath("//input[type=radio]")); int size = = radioButtons.size();
// Iterate the above element and use isSelected() method to identify the selected radio elements //迭代上述元素,并使用isSelected()方法标识选定的单选元素
Hope this clarify 希望这澄清
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.