Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I click a checkbox that is present but not visible using Splinter? (Selenium/Python)

Source snippet:

<div>
<div class="checkbox checkbox-primary margin-bottom-5 margin-top-5">
<input id="item" name="item" type="checkbox" value="true"/>
<label for="item">Item_Text</label>
<input name="item" type="hidden" value="false"/>
</div>

I'm trying to click on this checkbox using Splinter with the following code:

browser.find_by_id('item')click()

I've also tried find_by_name. Both produce the following exception:

(<class 'selenium.common.exceptions.ElementNotVisibleException'>, ElementNotVisibleException()

Attempting to debug:

print browser.is_element_present_by_id(item), browser.find_by_id(item).visible

returns True, False

It looks like Splinter's having trouble clicking the box because the second input block shows type='hidden'. However, adding .first.click() does not fix the problem and I'm out of ideas. I might be missing something really simple!

like image 824
Mcdizzle Avatar asked Jan 29 '26 16:01

Mcdizzle


1 Answers

The following javascript execution fixed the problem: browser.execute_script('document.getElementsByName("%s")[0].checked=true' % item)

like image 136
Mcdizzle Avatar answered Feb 01 '26 07:02

Mcdizzle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!