Best way to click a HTML checkbox using selenium (python)?

2,252 views
Skip to first unread message

Grant McGovern

unread,
Apr 15, 2014, 8:14:38 PM4/15/14
to seleniu...@googlegroups.com
Alright so I have somewhat of a dilemma... 

I'm working on a Python script that goes out, hits a url, and clicks a bunch of checkboxes and hits submit. However, I'm having some difficulty clicking the checkbox. 

So far, I've been using selenium as my web-interactivity library. I chose this as it seems they had the best option. Here's a gif of what I'm trying to click below. You'll see that when you click one of the boxes, it turns yellow. 

<a href="https://imgflip.com/gif/865rs"><img src="//i.imgflip.com/865rs.gif" title="made at imgflip.com"/></a>

Here is an HTML example for the checkbox(s) I'm trying to click:
 
    <li class= "zone odd open night">...</li>
<li class= "zone even open night">...</li>
<li class= "zone odd open night">...</li>
 <label for="srr-2-1397538000">Room 226 1:00 AM</label>
 <input type="checkbox" name="srr-2-1397538000" id="srr-2-1397538000" value="Y" class="interactive">
 <span class-"drag-handle">...</span>
</li>
<li class="zone even open night">...</li>


When I click on the HTML, the html changes so that the class name has "reserving" in it:


    <li class= "zone odd open night">...</li>
<li class= "zone even open night">...</li>
<li class= "zone odd reserving night">...</li>
 <label for="srr-2-1397538000">Room 226 1:00 AM</label>
 <input type="checkbox" name="srr-2-1397538000" id="srr-2-1397538000" value="Y" class="interactive">
 <span class-"drag-handle">...</span>
</li>
<li class="zone even open night">...</li>


Here is what I've tried so far but to no avail (just to click that one box as an example):

driver = webdriver.Chrome(executable_path="/Users/grantmcgovern/Dropbox/Developer/Projects/StudyBug/StudyBug.Sheni/chromedriver")
driver.get(url)
assert "ZSR" in driver.title 

for i in range(10):
   try:
    mouse = webdriver.ActionChains(driver)
       checkbox = driver.find_element_by_xpath("//label[contains(text(),'Room 228 3:30 AM')]/following-sibling::input")
       mouse.move_to_element(checkbox).click()
       break
   except NoSuchElementException as e:
       print('retry in 1s.')
       time.sleep(1)
else:
raise e


It simply just doesn't do anything, when it clearly should be clicking the checkbox, (and I would know if it was doing it because the box would turn yellow). 

Can anyone give me some pointers as to why it's not working or suggest another method to do this?

Thank You guys


If you would like to see the same question I posted on Stack Overflow, here is the link to it:

Doug Dragon

unread,
Apr 16, 2014, 1:37:14 PM4/16/14
to seleniu...@googlegroups.com
Hey there Grant,
From a quick look, it appears the checkbox variable is pointing to the label and not the checkbox (input) locator. You could try something like this:

checkbox = driver.find_element_by_css_selector("input#srr-2-1397538000")
checkbox.click()

I could be wrong about the above. Please post back to let me know if I'm wrong or that it didn't work.
Thanks!

-Doug
Reply all
Reply to author
Forward
0 new messages