【问题标题】:How to select radio button when two or three radio buttons are there, using selenium当有两个或三个单选按钮时如何选择单选按钮,使用硒
【发布时间】:2011-08-14 17:47:57
【问题描述】:

我正在寻找使用 java 的 selenium 代码,了解如何在表单中有多个单选按钮时选择特定单选按钮。

对于一个单选按钮,selenium.click("radio1") 可以,但在上述情况下

I.E.,我正在阅读 excel 表格

请在这方面帮助我

【问题讨论】:

    标签: java selenium radio-button


    【解决方案1】:

    您可以有多个同名的单选按钮。因此,您将需要通过 id 属性(每个元素必须是唯一的)或基于 value 属性(我只能假设它是不同的)进行选择...或通过位置索引(但这是一种有点脆弱的方法)

    例如使用类似的东西

    selenium.click("id=idOfItem");
    selenium.click("xpath=//input[@value='Blue']");//select radio with value 'Blue'
    

    【讨论】:

      【解决方案2】:

      使用selenium.check("name=<name> value=<value>");

      请注意,<name> 对于所有按钮都是相同的,但 <value> 会有所不同。

      【讨论】:

        【解决方案3】:
        // get all the radio buttons by similar id or xpath and store in List    
        List<WebElement> radioBx= driver.findElements(By.id("radioid"));
        // This will tell you the number of radio button are present
        int iSize = radioBx.size();
        //iterate each link and click on it
        for (int i = 0; i < iSize ; i++){
        // Store the Check Box name to the string variable, using 'Value' attribute
        String sValue = radioBx.get(i).getAttribute("value");
        // Select the Check Box it the value of the Check Box is same what you are looking for
            if (sValue.equalsIgnoreCase("Checkbox expected Text")){
             radioBx.get(i).click();
             // This will take the execution out of for loop
                break;
            }
           }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-02-21
          • 2018-10-27
          • 2016-09-24
          • 2015-02-27
          • 2014-03-07
          • 1970-01-01
          • 2014-11-23
          相关资源
          最近更新 更多