如何根据提供的HTML验证是否选择了单选按钮?

时间:2018-08-01 11:23:27

标签: java selenium selenium-webdriver xpath css-selectors

这是选定单选按钮的html代码

<input name="blooms_level" value="4" id="blooms_level4" class="radio " checked="" type="radio">

我尝试了下面的代码,其返回null

String str = driver.findElement(By.xpath("xpathValue")).getAttribute("checked");
            str.equalsIgnoreCase("true");

由于检查值为空,请提出其他替代方法

3 个答案:

答案 0 :(得分:1)

尝试下面提到的Java代码:

Boolean radioSelected= driver.findElement(By.xpath("xpathValue")).isSelected();
     if (radioSelected)
    {
      System.out.println("Radio Button is selected");
    }else{
      System.out.println("Radio Button is not selected");
    }

答案 1 :(得分:0)

要验证是否选择了单选按钮,可以使用以下任一解决方案:

if(driver.findElement(By.xpath("//input[@class='radio' and @name='blooms_level'][starts-with(@id,'blooms_level')]")).isSelected())
//if(driver.findElement(By.cssSelector("input.radio[name='blooms_level'][id^='blooms_level']")).isSelected())
    System.out.println("Radio Button is selected");
else
    System.out.println("Radio Button is not selected");

答案 2 :(得分:0)

您可以做一些简单的事情

Assert.assertTrue(driver.findElement(By.id("blooms_level4")).isSelected(), "Verify checkbox is checked");

带有TestNG