程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

使用 selenium webdriver 在 <label> 列表中查找选定的单选按钮

发布于2022-09-06 20:49     阅读(1250)     评论(0)     点赞(14)     收藏(1)


如何遍历label. div我的意思是有一堆未知数量的标签标签,它们又具有单选按钮。使用Selenium WebDriver我需要找到选定的radio button. 这里有两件事:

  1. 我需要找到无线电元素的数量
  2. 我需要找到选定的单选元素

例如

<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/

来源:前端黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

14 0
收藏该文
已收藏

评论内容:(最多支持255个字符)