Huy
Huy

Reputation: 11206

Capybara cannot detect element even though it is visible on page

I am using Capybara to manually fill out a page, however I am running into an issue.

When it tries to execute this line (I tried also setting visible to false):

find(:css, "[value='Laundry In Building']", visible: true).set(true)

It gets the following error:

Unable to find css "[value='Laundry In Building']"

This is confusing because I see the field in the browser when the test is running. Furthermore, when I output page.body, I get the following:

<div class="listing-edit-cb-item">
  <input class="cb" id="building_features_3" name="features[]"value="Laundry In Building" type="checkbox" />
  <label for="building_features_3">
    <span class="cb"></span>
    <span style="width: 200px; display: inline-block; font-size: 0.90em;">Laundry In Building</span>
  </label>
</div>

Why is Capybara no table to detect the checkbox element?

Upvotes: 0

Views: 870

Answers (1)

Thomas Walpole
Thomas Walpole

Reputation: 49950

You don't show the css applied to the checkbox, but I'll bet that it's not actually the checkbox you're seeing on the page. Use your browsers dev console to take a look at whats actually shown on the page, it's probably using pseudo elements on the span.cb to replace the checkbox with images so they look the same across multiple browsers. If you're using a recent release of Capybara you can do

check('Laundry In Building', allow_label_click: true)  # found by labels text

or

check(option: 'Laundry In Building', allow_label_click: true) # found by checkbox inputs value property

which will check the box by clicking on the visible label rather than the actual input that is hidden. If using an older version you'd need to find the label instead and call click on it. Something like

find(:css, 'label', text: 'Laundry In Building').click

Upvotes: 1

Related Questions