简体   繁体   English

在<label>使用Selenium WebDriver</label>的列表中找到选定的单选按钮

[英]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: 这里有两件事:

  1. I need to find the number of radio elements 我需要找到无线电元素的数量
  2. I need to find the selected radio element 我需要找到所选的广播元素

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.

相关问题 如何检查单选按钮是否在 Selenium WebDriver 中被选中? - How to check if the radio button is selected or not in Selenium WebDriver? 找出使用Selenium选择的单选按钮(实现为li) - Find out which radio button is selected using Selenium (implemented as li) 如何使用 Selenium WebDriver 读取 ::after 单选按钮元素的属性 - How to read ::after attributes of a radio button element using Selenium WebDriver 在Java中使用Selenium WebDriver确定单选按钮值 - Determining Radio Button Value Using Selenium WebDriver in Java 为什么我没有使用selenium webdriver选择单选按钮? - Why am failing to select radio button using selenium webdriver? 在视野之外使用 selenium webdriver java 选择单选按钮 - Selecting a radio button using selenium webdriver java when out of view 如何使用 selenium webdriver 在 JQGrid 中选择单选按钮 - How to select Radio button in JQGrid using selenium webdriver 如何使用带有Java的Selenium WebDriver选择单选按钮? - How to select radio button using Selenium WebDriver with Java? 如何通过不使用x路径来选择Selenium Webdriver中的单选按钮 - how to select radio button in selenium webdriver by not using x path 使用带有Java的Selenium WebDriver在组中选择单选按钮 - Select Radio Button in a group using Selenium WebDriver with Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM