升级到Capybara 2.0后,如何单击项目列表中的第一个链接? [英] How to click first link in list of items after upgrading to Capybara 2.0?

查看:31
本文介绍了升级到Capybara 2.0后,如何单击项目列表中的第一个链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这种情况下如何单击第一个链接:

How to click first link in that case:

<div class="item">
  <a href="/agree/">Agree</a>
</div>
<div class="item">
  <a href="/agree/">Agree</a>
</div>



within ".item" do
  first(:link, "Agree").click
end

并且我得到这个错误:

Capybara::Ambiguous:
  Ambiguous match, found 2 elements matching css ".item"

如果没有,我会得到这个错误:

And without the within I get this error:

Failure/Error: first(:link, "Agree").click
NoMethodError:
  undefined method `click' for nil:NilClass


推荐答案

您可以使用:

first('.item').click_link('Agree')

first('.item > a').click

(如果默认选择器为:css)

(if your default selector is :css)

您问题中的代码不起作用:

Code in your question doesn't work as:

within ".item" do
  first(:link, "Agree").click
end

等同于:

find('.item').first(:link, "Agree").click

水豚发现了几个 .item ,因此引发了异常。我认为水豚2的这种行为非常好。

Capybara finds several .item's so it raises an exception. I consider this behavior of Capybara 2 very good.

这篇关于升级到Capybara 2.0后,如何单击项目列表中的第一个链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆