【问题标题】:How to read the text from the selected radio button and how to select the other radio button?如何从选定的单选按钮中读取文本以及如何选择另一个单选按钮?
【发布时间】:2018-12-11 14:31:40
【问题描述】:

我想阅读选定的单选按钮文本,我想选择另一个单选框,所以我正在阅读所有 3 个单选按钮文本并传递给字符串并尝试选择未选中的单选按钮,但使用 if 条件 not能够做到,请帮我选中复选框。

这是 HTML 代码

<ul class="custom-ul">
    <li class="custom-li custom-radio m-subscriptions-padding receive-VIP1">
        <input name="email-preferences" id="receive-VIP" type="radio" value="receive-VIP1">
        <input name="_D:email-preferences" type="hidden" value=" ">                       <label for="receive-VIP" class="custom-radio-label">
             <span class="label-alt-text">Receive special offers via email</span>
        </label>
    </li>
    <li class="custom-li custom-radio m-subscriptions-padding receive-fewer1">
        <input name="email-preferences" checked="checked" id="receive-fewer" type="radio" value="receive-fewer1" class="radio-checked">
        <input name="_D:email-preferences" type="hidden" value=" ">
        <label for="receive-fewer" class="custom-radio-label">
             <span class="label-alt-text">Receive fewer emails. We won't email you more than one time per week.</span>
        </label>
    </li>
    <li class="custom-li custom-radio m-subscriptions-padding receive-no-emails">
        <input name="email-preferences" id="receive-no-emails" type="radio" value="receive-no-emails">
        <input name="_D:email-preferences" type="hidden" value=" ">
        <label for="receive-no-emails" class="custom-radio-label">
             <span class="label-alt-text"> Unsubscribe: we will remove you from all testdevv promotional emails.</span>
        </label>
        </li>
</ul>

【问题讨论】:

  • 这些是 3 li 标签3 个单选按钮
  • 不,在我的情况下,我想阅读选定的单选按钮文本,我想选择任何其他未从 3 中选择的单选按钮
  • 标签: selenium selenium-webdriver


    【解决方案1】:

    您可以尝试下一个解决方案:

        //this is row with radiobutton and text
        private By customRadioElement = By.xpath("//li[contains(@class, 'custom-radio')]");
        //this is all radiobuttons
        private By radiobutton = By.xpath("./input[@name='email-preferences']");
        //this is all labels for radiobuttons
        private By radiobuttonLabel = By.xpath("./label/span");
    
        public void selectRadiobutton(String labelText) {
            List<WebElement> tempList = driver.findElements(customRadioElement);
            for (WebElement element : tempList) {
                //webElement can search for elements, and if xpath locator starts with "." element will search for element from his children
                if (labelText.equals(element.findElement(radiobuttonLabel).getText())) {
                    element.findElement(radiobutton).click();
                }
            }
        }
    
        public String getSelectedRadiobuttonLabelText() {
            List<WebElement> tempList = driver.findElements(customRadioElement);
            for (WebElement element : tempList) {
                //webElement can search for elements, and if xpath locator starts with "." element will search for element from his children
                if (element.findElement(radiobutton).isSelected()) {
                    return element.findElement(radiobuttonLabel).getText();
                }
            }
            return "";
        }
    

    【讨论】:

      【解决方案2】:

      您可以在下面找到选定的选项文本和所有可用选项列表。

      请在您的代码中使用以下逻辑

      代码:

          //If none of the option is selected, then exception will be thrown and to avoid this we can use findElements method
          List<WebElement> radioButtonSelectedList=driver.findElements(By.xpath("//*[@class='radio-checked']//parent::*//span"));
          String selectedValue="";
          ArrayList<String> optionsList = new ArrayList<String>();
      
          if(radioButtonSelectedList.size()!=0){
              //If the button is selected, then we can get the selected option text
              selectedValue=radioButtonSelectedList.get(0).getText();
      
              //To get all the available Options
              List<WebElement> optionsElement=driver.findElements(By.xpath("//ul[@class='custom-ul']//span"));
      
              for(WebElement element: optionsElement){
                  optionsElement.add(element);
              }
      
          }
          else{
              System.out.println("None of the option is selected");
          }
      
          System.out.println("Available Options :"+optionsList);
          System.out.println("Selected Option :"+selectedValue);
      

      【讨论】:

        【解决方案3】:

        试试-

        import java.util.List;
        
        import org.openqa.selenium.By;
        import org.openqa.selenium.WebDriver;
        import org.openqa.selenium.WebElement;
        import org.openqa.selenium.chrome.ChromeDriver;
        
        public class A {
        
            public static void main(String[] args) {
                WebDriver driver = new ChromeDriver();
                driver.get("url here");
                List<WebElement> radioButtons = driver.findElements(By.name("email-preferences"));
                for (int i = 0; i < radioButtons.size(); i++) {
                    if (driver.findElement(By.name("email-preferences")).isSelected()) {
                        System.out.println(radioButtons.get(i).getText());
                        break;
                    }
                }
            }
        
        }
        

        【讨论】:

          猜你喜欢
          相关资源
          最近更新 更多
          热门标签