How do i click this button in capybara

2019-04-18 06:07发布

问题:

Please help me solve this problem with capybara

I have a button like this in capybara:

<input type="submit" value="Verify" name="verify" id="verify" class="button">

I tried with

click_button "verify"

but it gives error:

Failure/Error: find('#verify').click
NoMethodError:
  undefined method `node_name' for nil:NilClass

回答1:

Answer by the author

The problem lies inside the html code:

<div>
<form>
<div>    
</div>
</div>
  <input type="submit" value="Verify" name="verify" id="verify" class="button">
</form>

Because there is one redundant </div>, the <input> was treat outside the form, hence capybara cause those error. After delete the redundant </div>, everything works fine.



回答2:

Try adding js: true in the describe. This happens when you do not have a form that contains the button.



回答3:

Did you try "doubling up" the CSS selectors? This has been my go-to mechanism since capybara-2.4.3

find("#verify").find("[name=verify]").click

any other attribute in addition to the #id-vale should do the trick, e.g

find("#verify").find(".button]").click


回答4:

If you have an ID for an element, just use @bonzofenix's approach but make it a bit more simple:

within 'form' do find('#verify').click end