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

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

问题描述

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

<a href="/agree/">同意</a>

<div class="item"><a href="/agree/">同意</a>

在.item"中做首先(:链接,同意").点击结尾

我收到此错误:

Capybara::Ambiguous:不明确的匹配,找到 2 个匹配 css.item"的元素

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

失败/错误:first(:link, "Agree").click无方法错误:nil:NilClass 的未定义方法`click'

解决方案

您只需使用:

first('.item').click_link('同意')

first('.item > a').click

(如果你的默认选择器是 :css)

<小时>

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

在.item"中做首先(:链接,同意").点击结尾

相当于:

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

Capybara 发现了多个 .item,因此它引发了异常.我认为 Capybara 2 的这种行为非常好.

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

and I get this error:

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

解决方案

You can just use:

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

or

first('.item > a').click

(if your default selector is :css)


Code in your question doesn't work as:

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

is equivalent to:

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

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天全站免登陆