簡體   English   中英

在<label>使用Selenium WebDriver</label>的列表中找到選定的單選按鈕

[英]Find the selected radio button in a list of <label> using selenium webdriver

如何遍歷div所有label項。 我的意思是有一堆未知數量的標簽標簽,這些標簽標簽依次具有單選按鈕。 使用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();
    }
  }

希望能幫助到你...:)

//這將給出無線電元素的數量

List<WebElement> radioButtons = driver.findElements(By.xpath("//input[type=radio]")); int size = = radioButtons.size();

//迭代上述元素,並使用isSelected()方法標識選定的單選元素

希望這澄清

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM