How can I check a checkbox that is present but not displayed using Splinter? (Selenium / Python)

Original 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 am trying to click on this checkbox using Splinter with the following code:

browser.find_by_id('item')click()

      

I also tried find_by_name. Both have 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 is having problems with the button click because the second input block is showing type = 'hidden'. However, adding .first.click () does not fix the problem and I am out of ideas. I might be missing something very simple!

+3


source to share


3 answers


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



+2


source


I usually use xpath

this in this case as label

these checkboxes are unique and more secure to identify the item correctly. However, you need to make sure the ids are not duplicated. This is the main part of having IDs for testing Selenium

. try this:

//label[contains(text(),'Item_Text')]/../input[@type='checkbox']

      

Using

browser.find_by_xpath("//label[contains(text(),'Item_Text')]/../input[@type='checkbox']")

      

The above item should find the item based on the Item_Text label



Edit

Try to grab the list and then look at the displayed one and press

elements = browser.find_by_xpath("//label[contains(text(),'Item_Text')]/../input[@type='checkbox']")
for element in elements:
    if element.is_displayed():
        element.click()
        break

      

Splinter syntax may differ slightly. Please refer to the doc as needed.

0


source


This is a known issue where web code is "tampered with" on JavaScript

. @Mcdizzle's solution Selenium

is to fix the click through JavaScript

:

browser.execute_script('document.getElementById("%s").click()' % id_string)

      

0


source







All Articles