86

I am using Selenium in Java to test the checking of a checkbox in a webapp. Here's the code:

private boolean isChecked;
private WebElement e;

I declare e and assign it to the area where the checkbox is.

isChecked = e.findElement(By.tagName("input")).getAttribute("checked").equals("true");

What is weird is that getAttribute("checked") returns null and therefore a NullPointerException

In the HTML for the checkbox, there is no checked attribute displayed. However, isn't it the case that all input elements have a checked = "true" so this code should work?

Josh Correia
  • 3,807
  • 3
  • 33
  • 50
jamesfzhang
  • 4,403
  • 8
  • 32
  • 43

8 Answers8

149

If you are using Webdriver then the item you are looking for is Selected.

Often times in the render of the checkbox doesn't actually apply the attribute checked unless specified.

So what you would look for in Selenium Webdriver is this

isChecked = e.findElement(By.tagName("input")).Selected;

As there is no Selected in WebDriver Java API, the above code should be as follows:

isChecked = e.findElement(By.tagName("input")).isSelected();
Matthijs Wessels
  • 6,530
  • 8
  • 60
  • 103
CBRRacer
  • 4,649
  • 1
  • 23
  • 27
  • Terrific! I can't believe I didn't see the `isSelected()` method in the API. I was essentially trying to write my own method for it when it already existed. Thank you. – jamesfzhang Nov 18 '11 at 20:45
  • 1
    IMHO the naming convention in the API is actually not good - "selecting" suggests there is a list of things to select from, like for HTML Select tag. Checkbox is binary, checked or not. – CJ Kepinsky Nov 21 '19 at 09:03
16
if ( !driver.findElement(By.id("idOfTheElement")).isSelected() )
{
     driver.findElement(By.id("idOfTheElement")).click();
}
ShutterSoul
  • 2,551
  • 3
  • 23
  • 28
11
 if(checkBox.getAttribute("checked") != null) // if Checked 
    checkBox.click();                         //to Uncheck it 

You can also add an and statement to be sure if checked is true.

Flexo
  • 87,323
  • 22
  • 191
  • 272
Aditya
  • 111
  • 1
  • 2
9

I would do it with cssSelector:

// for all checked checkboxes
driver.findElements(By.cssSelector("input:checked[type='checkbox']"));
// for all notchecked checkboxes
driver.findElements(By.cssSelector("input:not(:checked)[type='checkbox']"));

Maybe that also helps ;-)

WoodenKitty
  • 6,521
  • 8
  • 53
  • 73
Tarken
  • 2,112
  • 2
  • 23
  • 42
  • This does not answer the question. It was not the question how to search for a checked checkbox. The question was if any checkbox is checked or not. – Elmue Dec 05 '15 at 03:41
2

For the event where there are multiple check-boxes from which you'd like to select/deselect only a few, the following work with the Chrome Driver (somehow failed for IE Driver):

NOTE: My check-boxes didn't have an ID associated with them, which would be the best way to identify them according to the Documentation. Note the ! sign at the beginning of the statement.

if(!driver.findElement(By.xpath("//input[@type='checkbox' and @name='<name>']")).isSelected()) 
{
  driver.findElement(By.xpath("//input[@type='checkbox' and @name= '<name>']")).click();
}
Jamal
  • 763
  • 7
  • 22
  • 32
1
  1. Declare a variable.
  2. Store the checked property for the radio button.
  3. Have a if condition.

Lets assume

private string isChecked; 
private webElement e; 
isChecked =e.findElement(By.tagName("input")).getAttribute("checked");
if(isChecked=="true")
{

}
else 
{

}

Hope this answer will be help for you. Let me know, if have any clarification in CSharp Selenium web driver.

Balaji
  • 21
  • 3
1
public boolean getcheckboxvalue(String element)
    {   
        WebElement webElement=driver.findElement(By.xpath(element));
        return webElement.isSelected();
    }
Kursad Gulseven
  • 1,978
  • 1
  • 24
  • 26
  • 1
    It is a good idea to use the `isSelected()` method. Yet your code does not directly answer the question. It would be good if you changed it to match the code in the top post or explain (in English) what changes you recommend. – Dima Chubarov Feb 14 '17 at 14:37
  • element is the xpath which you would be sending on calling the function. the return value will be the state of check box. – akhilesh gulati Feb 14 '17 at 16:18
-1

The mechanism of selenium framework:

Here selenium make request to the its server and fetch first subelement with tagname input

WebElement e = e.findElement(By.tagName("input"));

Than you try to receive attribute on that element

object attribute = e.getAttribute("checked")

So either use

findElement(By.attribute("checked")

or use

findElement(By.xpath("\\input[@checked='true']")

P.S. I'm not familiar with java's equivalent of selenium api so some method may be named slightly different.

VMykyt
  • 1,589
  • 12
  • 17
  • This doesn't really address the issue. I am trying to determine if a checkbox is selected, hence the boolean `isSelected`. I am not trying to find a checkbox that is selected. Anyways I'll just be using a workaround by surrounding `isSelected` with a `try` and `catch` on a `NullPointerException` – jamesfzhang Nov 18 '11 at 19:33