Find out if a checkbox is selected

11,002 views
Skip to first unread message

Shaun Wiseman

unread,
May 10, 2012, 5:22:25 AM5/10/12
to Selenium Users
I have been using .isSelected() method to see if the box is selected
but it doesnt work it returns null for all of them and im not sure how
to fix it. Is there a better way to see if they are selected?

Thanks,
Shaun

Tarun Kumar

unread,
May 10, 2012, 5:30:59 AM5/10/12
to seleniu...@googlegroups.com
Could you post snippet of you html and your web driver code?

Shaun Wiseman

unread,
May 10, 2012, 5:36:40 AM5/10/12
to Selenium Users
//Java
//Keeps returning null pointer so try/cath added
try{
web=driver.findElement(By.xpath("//input[@name='GPP']"));
check=web.isSelected();
assertTrue(check);
}
catch (Throwable e)
{}
/////
Html:

<div class="x-form-check-wrap" id="ext-gen316" style="width: 89px;">

<input type="checkbox" name="GPP" id="ext-comp-1065"
autocomplete="off" class=" x-form-checkbox x-form-field" value="GPP">
<label class="x-form-cb-label" for="ext-comp-1065" id="ext-
gen317">GPP</label></div>

Tarun Kumar

unread,
May 10, 2012, 5:42:57 AM5/10/12
to seleniu...@googlegroups.com
Don't see any thing obviously wrong. 
I suppose you get null pointer exception at -

 web=driver.findElement(By.xpath("//input[@name='GPP']")); 

Are you sure driver is not null at this line?

Shaun Wiseman

unread,
May 10, 2012, 5:51:54 AM5/10/12
to Selenium Users
Possitive I have checked for it in the line before that and it finds
it no problem.

Rajat Jindal

unread,
May 10, 2012, 6:19:50 AM5/10/12
to seleniu...@googlegroups.com

My 2 cents.

Can you confirm if the xpath //input[@name='GPP'] returns just one element. [More then one input element with name 'GPP' may be present in your webpage] or if it returns element at all. Please verify it using, may be, web.getAttribute('GPP').

Shaun Wiseman

unread,
May 10, 2012, 6:24:45 AM5/10/12
to Selenium Users
I have been using findEmelment(By.xpath(" //input[@name='GPP'")). In
the web driver if it doesnt find it it will error and go to the next
test

Maxim Vorobev

unread,
May 10, 2012, 6:39:59 AM5/10/12
to seleniu...@googlegroups.com
In same cases isSelected() method doesn't work. If you use IE - open developer tool (F12) and check all node's attributes (include read-only) to find witch of them can be used to identify checkbox state.

2012/5/10 Shaun Wiseman <shaun.w...@gmail.com>
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.


Shaun Wiseman

unread,
May 10, 2012, 6:48:36 AM5/10/12
to Selenium Users
is there a way around this?

On May 10, 11:39 am, Maxim Vorobev <vmaximw...@gmail.com> wrote:
> In same cases isSelected() method doesn't work. If you use IE - open
> developer tool (F12) and check all node's attributes (include read-only) to
> find witch of them can be used to identify checkbox state.
>
> 2012/5/10 Shaun Wiseman <shaun.wisema...@gmail.com>

Rajesh Ratakonda

unread,
May 10, 2012, 7:04:23 AM5/10/12
to seleniu...@googlegroups.com
HI ,
 
Can you try it using the driver.findElement(By.id("ext-comp-1065")).
 
Regards,
Rajesh.

Shaun Wiseman

unread,
May 10, 2012, 7:09:28 AM5/10/12
to Selenium Users
ext-comp-1065 is an auto generated ID. The next time the java servelet
is called it could be ext-comp-????....anything really

Rajat Jindal

unread,
May 10, 2012, 7:25:29 AM5/10/12
to seleniu...@googlegroups.com
If you are sure there will always be one element with name GPP, then try following:

driver.executeScript("return document.getElementsByName('GPP')[0].checked;");

It will return true if the checkbox is checked and false otherwise. [Please note that this will skip other validations such as element present but not displayed etc]

Shaun Wiseman

unread,
May 10, 2012, 7:46:42 AM5/10/12
to Selenium Users
No luck. The error that was reported is "null"

Maxim Vorobev

unread,
May 10, 2012, 7:57:39 AM5/10/12
to seleniu...@googlegroups.com
1. Be sure that driver finds checkbox properly:
web=driver.findElement(By.xpath("//input[@name='GPP']"));
web.click();
 
2. If web.click() check/uncheck  checkbox, read my post above,
and take a look into selenium-source:
 /**
* Returns whether the element is checked or selected.
*
* @param {!Element} element The element to check.
* @return {boolean} Whether the element is checked or selected.
*/
bot.dom.isSelected = function(element) {
if (!bot.dom.isSelectable(element)) {
throw new bot.Error(bot.ErrorCode.ELEMENT_NOT_SELECTABLE,
'Element is not selectable');
 } 
var propertyName = 'selected';
var type = element.type && element.type.toLowerCase();
if ('checkbox' == type || 'radio' == type) {
propertyName = 'checked';
}

return !!bot.dom.getProperty(element, propertyName);
};
 So, if <input type='checkbox'> hasn't 'checked' property, you can't use isSelected() method and you should find valid attribute and use them.

2012/5/10 Shaun Wiseman <shaun.w...@gmail.com>
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.

Mark Collin

unread,
May 10, 2012, 11:23:21 AM5/10/12
to seleniu...@googlegroups.com
I've not come across .isSelected() not working when it is used properly
(Maxim, please feel free to prove me wrong with some reproducible steps).

The problem appears to be in finding the element, not finding out if the
element is selected or not. Can you show us the HTML so we can see if your
XPath is sane.

Assuming the XPath is sane I suspect the problem is that the element has not
loaded when you are trying to check it, the way around this would either be
to put in an explicit wait, or use implicit waits e.g.:

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

Mark Collin

unread,
May 10, 2012, 11:25:49 AM5/10/12
to seleniu...@googlegroups.com

If there is more than one element that matches a locator Selenium/WebDriver will always return the first instance of the element that it finds.  In this case you would still get a true/false response from .isSelected().

 

 

 

From: seleniu...@googlegroups.com [mailto:seleniu...@googlegroups.com] On Behalf Of Rajat Jindal
Sent: 10 May 2012 11:20
To: seleniu...@googlegroups.com
Subject: [selenium-users] Re: Find out if a checkbox is selected

 

My 2 cents.

Can you confirm if the xpath //input[@name='GPP'] returns just one element. [More then one input element with name 'GPP' may be present in your webpage] or if it returns element at all. Please verify it using, may be, web.getAttribute('GPP').

--

You received this message because you are subscribed to the Google Groups "Selenium Users" group.

To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/AmtcICcbEC8J.

Mark Collin

unread,
May 10, 2012, 11:29:19 AM5/10/12
to seleniu...@googlegroups.com

I would suggest not executing your own JavaScript instead of using the built in functions supplied by Selenium/WebDriver, they have been written, tried, and tested over a number of years and are going to be much more stable/accurate than anything you hand craft.  If there is a problem with them raise a bug so it can be fixed and you don’t have to maintain your own JavaScript codebase.

 

If you insist on doing this there is little point in using selenium in the first place.

 

From: seleniu...@googlegroups.com [mailto:seleniu...@googlegroups.com] On Behalf Of Rajat Jindal
Sent: 10 May 2012 12:25
To: seleniu...@googlegroups.com
Subject: [selenium-users] Re: Find out if a checkbox is selected

 

If you are sure there will always be one element with name GPP, then try following:

--

You received this message because you are subscribed to the Google Groups "Selenium Users" group.

To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/oIVE39wYYMUJ.

Mike Riley

unread,
May 10, 2012, 2:11:28 PM5/10/12
to seleniu...@googlegroups.com
Shaun,

I have been using WebDriverBackedSelenium for most of my code, simply because I find it a convenient way to get at wrapper methods for WebDriver.  I have been using the selenium.isChecked(locatorString) method and it is working just fine for me.  You might want to give it a try.

Mike

Maxim Vorobev

unread,
May 11, 2012, 3:13:50 AM5/11/12
to seleniu...@googlegroups.com
Hi Mark! 
Maxim, please feel free to prove me wrong with some reproducible steps
 
Check this one:

WebDriver driver = new InternetExplorerDriver();
WebDriverBackedSelenium rc = new WebDriverBackedSelenium(driver, "");
if (driver.getWindowHandles().size()==0){
throw new RuntimeException("Hello!");
}
String xpath = "//INPUT[@type='checkbox' and @id='cbt_CheckBox_1']";
new WebDriverWait(driver, 5000).until(ExpectedConditions.presenceOfElementLocated(By.xpath(xpath)));
WebElement checkbox = driver.findElement(By.xpath(xpath));
System.out.println("============Before click");
System.out.println("isSelected via webdriver: "+checkbox.isSelected());
System.out.println("isSelected via rc: "+rc.isChecked(xpath));
System.out.println("isSelected via value attribute: "+checkbox.getAttribute("value"));
checkbox.click();
System.out.println("============After click");
System.out.println("isSelected via webdriver: "+checkbox.isSelected());
System.out.println("isSelected via rc: "+rc.isChecked(xpath));
System.out.println("isSelected via value attribute: "+checkbox.getAttribute("value")); 

Output:
============Before click
isSelected via webdriver: true
isSelected via rc: true
isSelected via value attribute: mixed
============After click
isSelected via webdriver: true
isSelected via rc: true
isSelected via value attribute: unchecked
2012/5/10 Mike Riley <lvsk...@cox.net>

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/DqcR-iqJiPsJ.

Mark Collin

unread,
May 11, 2012, 4:12:55 AM5/11/12
to seleniu...@googlegroups.com

Had a quick look (and it needs more investigation) but I would say that you have a bug with the checkbox implementation there,

 

The checked attribute is not changed when you check/uncheck the checkbox (I manually tested this using firebug), instead it is modifying the value, and a custom attribute called aria-checked.  I haven’t investigated the JavaScript that is being fired by the onCheck event but my guess is that modification of the checked attribute is being suppressed so that the value attribute and the custom  attribute aria-checked is used instead.

 

In this case the checked attribute of the input element is always set so if you check to see if it is set or not it should always return true.  Selenium/WebDriver is doing this correctly and reporting the state of the checked attribute correctly. 

 

There is no bug with Selenium here, there is a bug with the implementation of the checkbox control and by working around it you are effectively coding your test to deal with invalid behaviour rather than getting a bug fixed.

Shaun Wiseman

unread,
May 11, 2012, 4:16:20 AM5/11/12
to Selenium Users
How do you create a selenium object in the web driver?

Mark Collin

unread,
May 11, 2012, 4:27:17 AM5/11/12
to seleniu...@googlegroups.com

I would also highlight that the only valid values for the attribute checked are:

 

“checked”

“”

 

It used to be just the presence of a checked attribute that was validated, but to make it XML compliant it had to be modified to have a value.

Mike Riley

unread,
May 11, 2012, 11:49:17 AM5/11/12
to seleniu...@googlegroups.com
As easily as this:
selenium = new WebDriverBackedSelenium(driver, baseUrlString);

Mike

Shaun Wiseman

unread,
May 14, 2012, 5:52:17 AM5/14/12
to seleniu...@googlegroups.com
Thanks Mike that worked a charm :)

Shaun

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/oXNFHr9cs2sJ.

Satish Kushwaha

unread,
Oct 10, 2013, 7:47:43 AM10/10/13
to seleniu...@googlegroups.com
Can anybody tell me how to select the checkbox in MAIL.AOL by using selenium webdriver?
Reply all
Reply to author
Forward
0 new messages