Selenium Java,如何检查是否选择了多个单选按钮?

时间:2016-04-01 15:34:38

标签: java selenium-webdriver

我正在研究一个框架,并尝试了以下而不是结果:

List<WebElement> rows = EarningNormal.oRdioList;             
java.util.Iterator<WebElement> i = rows.iterator();         
while(i.hasNext()) {                
    WebElement ContribIDYES = i.next();         
   //System.out.println(sitcodes.getText());                            
    if(ContribIDYES.isSelected()){
       TestDriver.globalProps.getHtmlReport().writeHTMLReport("Contribution ID Formulas must be set to Yes as default", "Contribution IDs must be set to YES ", "All Contribution IDs must be YES","Contribution ID's are set to YES" , "PASS", "Done");            
    }                   
    else{
       TestDriver.globalProps.getHtmlReport().writeHTMLReport("Contribution ID Formulas must be set to Yes as default", "Contribution IDs must be set to YES ", "All Contribution IDs must be YES", "Contribution ID's are NOT seto to YES as default", "FAILED",TestDriver.comUtil.getImageFileLoc(TestDriver.globalProps.getWebDriver()));
    }
}

1 个答案:

答案 0 :(得分:0)

嗨,如果您想验证是否选择了单选按钮,请注意任何带有电台类型的输入标签都有隐藏的属性值,如果选择了单选按钮,则称为已选择它的值是=如果不是那么它的值是= null,因此在此基础上你可以很容易地识别出哪个单选按钮被选中。下面找到一个相同的工作示例

WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        driver.get("C:\\Users\\rajnish\\Desktop\\myradio.html");

        // working with radio buttons
        // first take all the radio buttons inside a list like below
        List<WebElement> myradioloc = driver.findElements(By.xpath("//*[@type='radio']"));

        // apply the for loop to identify/ verify if a radio button is selected or not
        for(int i=0;i<myradioloc.size();i++){
            System.out.println("attribut value Selected of radio button is : " + myradioloc.get(i).getAttribute("selected"));
            // if radio button is selected then value of selected attribute is True else null
            if( myradioloc.get(i).getAttribute("selected").equals("null")){
                // as if loop will only run when value of selected attribute is null
                // i.e only when  radio button is not selected 
                myradioloc.get(i).click();
            }
        }