如何在selenium webdriver中使用类型 - 按钮验证单选按钮。

时间:2014-11-24 08:00:12

标签: forms selenium radio-button webdriver junit4

下面是单选按钮的html代码。网格形式绑定hrml页面中有三个单选按钮,我想验证是否选择了所需的无线电按钮。

我使用的webdriver代码是: -

//here totalDivions is an integer value which checks for the number of radio buttons.
for(int j=1;j<=totalDivisions;j++){
    String check = driver.findElement(By.xpath("//*[@id='radiogroup-1022-bodyEl']/table/tbody/tr/td["+j+"]/table/tbody/tr/td[2]/input")).getAttribute("checked");
        if(check.equalsIgnoreCase("true")){
            System.out.println("Checked");
    }
}

我尝试过使用isSelected()但没有用。

2 个答案:

答案 0 :(得分:0)

isSelected()是检查复选框是否已选中的正确方法。 看起来您的页面是使用某个JS框架生成的,并且在调用isSelected()方法时可能无法完全生成您的页面。 无论如何,你在调用它时会遇到异常吗?

答案 1 :(得分:0)

使用isSelected()方法尝试以下代码。

for(int j=1;j<=totalDivisions;j++){
    boolean check = driver.findElement(By.xpath("//*[@id='radiogroup-1022-bodyEl']/table/tbody/tr/td["+j+"]/table/tbody/tr/td[2]/input")).isSelected();
        if( !check )
            System.out.println("Checkbox is not Checked.");
        else
            System.out.println("Checkbox is Checked.");         
    }