How to check a checkbox nowadays

98 views
Skip to first unread message

Aaron

unread,
Oct 5, 2011, 5:06:35 PM10/5/11
to webdriver
Using Selenium 2.7.0

Hi, I used to use in previous versions of Webdriver:
el.setSelected(); or el.toggle();

on an input with type checkbox.

I have a situation now where I am doing
el.click();

on a checkbox, but the checkbox is not getting selected, and no errors
are thrown.

I printed the webelement so I know it found the element.

What's the best way to check a checkbox nowadays? Are we supposed to
use 'click'?

Aaron

unread,
Oct 5, 2011, 5:26:06 PM10/5/11
to webdriver
BTW, I can see that each input is gaining focus because the screen is
jumping around. I have a whole bunch of checkboxes that webdriver is
attempting to check. I am in a loop and checking each one like this:

WebElement anInput = null;
for ( String id : getInputIds() ) {
WebElement input = CmsTestCase.driver.findElement( By.id( id ) );
input.click();
anInput = input;
}

anInput.submit();

I don't see any rhyme or reason to which inputs are actually getting
selected. None of them are enabled to start with. Some get selected
and some don't, again no exceptions are thrown either when finding the
element by id or when clicking.


Luke Inman-Semerau

unread,
Oct 5, 2011, 6:11:34 PM10/5/11
to webd...@googlegroups.com
sounds like a bug... you need to include browser / os versions and a sample html file that reproduces the issue and log an new issue. (http://code.google.com/p/selenium/issues/entry)

You could also post back here for someone to try to reproduce your issue.

AFAIK click works for clicking checkboxes (does for me) and is the intended method for doing so.



--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.


Danny Guerrier

unread,
Oct 5, 2011, 9:39:29 PM10/5/11
to webd...@googlegroups.com
Try slowing it down by adding a pause between clicks in that loop. Maybe the browser is overwhelmed by all of the clicks. 

Aaron

unread,
Oct 6, 2011, 5:24:55 PM10/6/11
to webdriver
I found out that the problem is that the inputs are in a table, and
there's js on the page that makes the table headers stay at the top of
the page no matter where you scroll. In some cases the headers then
interfere with the input because of z-index, and the inputs can't gain
focus.

I guess I have to scroll the page down a bit first. Is there a way to
do that with webdriver API, or must I use straight JavaScript?

Aaron

unread,
Oct 6, 2011, 5:28:15 PM10/6/11
to webdriver
Actually, that won't work, because webdriver will scroll the element
to a certain location when I try to click. This used to work for me,
but something changed in the webdriver code re this.

The page is here:
http://www.ncbi.nlm.nih.gov/staff/mjohnson/cohenaa/webdriver/test-inputs.html

Aaron

unread,
Oct 7, 2011, 8:34:17 AM10/7/11
to webdriver
So the solution is to put a display:none; on the thead element of each
element before the click, which is a hacky solution. I think there
should be an optional parameter to click() that's an offset so you can
control how far up or down the scroll is before the click. Should I
put this feature request in, or wait for more discussion here?
Reply all
Reply to author
Forward
0 new messages