Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am learning Webdriver with JUnit by going through Alan Richardson's Selenium Simplified book and taking translating the exercises/tests from Selenium RC to Webdriver. So far, this has proven to be an excellent learning experience. But recently I have hit an issue, and cannot solve it despite extensive searching.

In Selenium RC there is a selenium.check command which will check a box if it is empty, or leave it if it is already checked. For example:

    selenium.check("//input[@name=’checkboxes[]’ and @value=’cb3’]");

If you go to the target website ( you will see there are three check boxes, one of which is checked. I have written some pseudo-code but just can't get into making it work. Here is the pseudo-code:

   isChecked = driver.findElement(By.xpath("//input[@type='checkbox']"));

   if (isChecked = false){
       check the box;
       do nothing;

I know the boolean "isSelected()" should feature, but I don't know where to go beyond that. All my research has returned partial solutions, but nothing that gets close enough for me to take a leap of faith at the solution.

share|improve this question
I am a little confused in what the issue exactly is. Are you not able to toggle the checkbox or are you not able to verify whether the checkbox is checked? Also if you haven't yet looked at, this doc might assist you -… – Suchit Parikh Dec 12 '12 at 18:23
@SheyMouse, it may help others helping you if you write down the code you tried, the expected result, and actual result, as you were reporting a bug ;-) – dzieciou Dec 12 '12 at 18:55
@SuchitParikh Thanks for the link. I am learning to read documentation, but application of the knowledge is still a bit rusty. – SheyMouse Dec 13 '12 at 10:43
@dzieciou Thank you for your advice. I had been attempting to do what you mentioned, but wasn't making progress. I am getting better at debugging my code, but this was one time I couldn't it fixed. – SheyMouse Dec 13 '12 at 10:45
up vote 5 down vote accepted

In the Selenium Simplified course the secret to the selector is really the 'value' not the 'type' as the 'value' identifies the WebElement uniquely on that page, coupled with a type 'just in case'

    WebElement checkBox1;
    WebElement checkBox3;

    checkBox1 = driver.findElement(By.cssSelector("input[value='cb1']"));
    checkBox3 = driver.findElement(By.cssSelector("input[value='cb3']"));


    //checkBox3 is selected by default

Or using findElements code

You could instead do

  List<WebElement> selectElements= driver.findElements(By.cssSelector("input[name='checkboxes[]']"));


    if( selectElements.get(2).isSelected()){

Or iterate over them

for(WebElement checkbox : selectElements){
    // uncheck 'em all

Hope that helps.

share|improve this answer

It should be as easy as:

IWebElement element = driver.findElement(By.xpath("//input[@type='checkbox']"));
if (!element.Selected)
share|improve this answer
Thank you for the example. Two things. First, what is the difference between IWebElement and just WebElement? Secondly, this only seems to check the first of three boxes and leaves a second box unchecked. Is that correct? My feeling is it should pick up all unchecked boxes and check them. Any further pointers would be appreciated. – SheyMouse Dec 13 '12 at 10:47
IWebElement is if you're using the .net webdriver. Ah, I didn't realize you wanted to check all 3 checkboxes. In that case you could do driver.findElements instead of driver.findElement and then put the code above in a foreach to iterate through each checkbox. – Sam Woods Dec 13 '12 at 21:26

This is a sample of working code - selecting sex by male and female radio buttons.

/* load that webelement list of radio buttons. then follow the below code & logic
List<WebElement> rdBtn_Sex = driver.findElements("sex"));

        // Create a boolean variable which will hold the value (True/False)
         boolean bValue = false;

         // This statement will return True, in case of first Radio button is selected
         bValue = rdBtn_Sex.get(0).isSelected();

         // This will check that if the bValue is True means if the first radio button is selected
         if(bValue = true){

             // This will select Second radio button, if the first radio button is selected by default


             // If the first radio button is not selected by default, the first will be selected
share|improve this answer

It is very Simple in C#

Example :

IWebElement chkBox = driver.FindElement(By.Id("some id "));
if (chkBox.Selected) {
  //perform actions 
} else {
  //perform actions
share|improve this answer
Not sure how this is different than Sam's answer. – Suchit Parikh May 3 '13 at 18:17

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.