Click Checkbox by Value Selenium

2.8k views Asked by At

I have google around a bit trying to finds some way you can click check boxes by entering a value, similar what you can use to select values in Drop Down List. But havent found a way yet.

I have two check boxes with Yes and No

div class="radio">
 <label>
   <input type="radio" name="LongTermContract" value="Yes" autocomplete="off" checked="">Yes
</label>
<label>
<input type="radio" name="LongTermContract" value="No" autocomplete="off">No                                </label>                          
</div>

I'm also using PageObjects,

[FindsBy(How = How.Name, Using = "LongTermContract")]
public IWebElement radioBtnLongTermContract { get; set; }

This is the Method I have.

    public static void SelectOptions(this IWebElement element, string value)
    {
        PropertiesCollection.driver.FindElement(By.XPath("//input[@value='" + value + "']")).Click();

    }

Now if im trying this

public void SelectValue(){
  Reporting("NO"); 
    }

I get an error that it does not find the element

Additional information: no such element: Unable to locate element: {"method":"xpath","selector":"//input[@value='NO']"}

Any ideas tips?

1

There are 1 answers

2
NarendraR On BEST ANSWER

This is the code snippet is in java. You need to pass value either Yes or No it will select respective radio button as the same value used for <input> tag's attribute value

public static void SelectOptions(String value)
{
   driver.findElement(By.xpath("//input[@value='"+value+"']")).click();
 }