Ruby-on-rails 我如何在水豚中点击这个按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10613354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 03:24:42  来源:igfitidea点击:

How do i click this button in capybara

ruby-on-railsruby-on-rails-3testingrspeccapybara

提问by jwall

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"

click_button "verify"

but it gives error:

但它给出了错误:

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

回答by Mark Huk

Answer by the author

作者的回答

The problem lies inside the html code:

问题出在 html 代码中:

<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 capybaracause those error. After delete the redundant </div>, everything works fine.

因为有一个多余的</div><input>被处理在表格之外,从而capybara导致那些错误。删除冗余后</div>,一切正常。

回答by bonzofenix

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

尝试js: truedescribe. 当您没有包含按钮的表单时会发生这种情况。

回答by Centzon

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

您是否尝试过“加倍”CSS 选择器?自 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

除了#id-vale 之外的任何其他属性都应该起作用,例如

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

回答by taystack

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

如果你有一个元素的 ID,只需使用@bonzofenix 的方法,但让它更简单一点:

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

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