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 (http://compendiumdev.co.uk/selenium/basic_html_form.html) 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;
} else {
    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.

  • 2
    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 - jarvana.com/jarvana/view/org/seleniumhq/selenium/… Dec 12, 2012 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, 2012 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, 2012 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, 2012 at 10:45

5 Answers 5


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= 


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

Or iterate over them:

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

Hope that helps.


It should be as easy as:

IWebElement element = driver.findElement(By.xpath("//input[@type='checkbox']"));
if (!element.Selected)
  • 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, 2012 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, 2012 at 21:26

It is very Simple in C#

Example :

IWebElement chkBox = driver.FindElement(By.Id("some id "));
if (chkBox.Selected) {
  //perform actions 
} else {
  //perform actions
  • 1
    Not sure how this is different than Sam's answer. May 3, 2013 at 18:17

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(By.name("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
} else {
     // If the first radio button is not selected by default, the first will be selected
Boolean checkedStatus = true;
checkedStatus = CheckBox1.Selected;
if (checkedStatus == false) {

I found as long I use "selected" it always returns false even though the checkbox was selected. Anyone who knows how to deal with it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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