Ash
Ash

Reputation: 69

How to validate if a radio button is selected or not as per the HTML provided?

Here is html code for selected radio button

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

i tried below code, its returning null

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

please suggest any alternate way since checked value is null

Upvotes: 0

Views: 1560

Answers (3)

JeffC
JeffC

Reputation: 25577

You can do something simple like

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

with TestNG.

Upvotes: 0

undetected Selenium
undetected Selenium

Reputation: 193078

To validate if the radio button is selected or not you can use either of the following solution:

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");

Upvotes: 0

Monika
Monika

Reputation: 732

Try the Java code mentioned below:

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");
    }

Upvotes: 1

Related Questions