ruby 升级到 Capybara 2.0 后如何单击项目列表中的第一个链接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14513377/
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-06 05:41:33  来源:igfitidea点击:

How to click first link in list of items after upgrading to Capybara 2.0?

rubyrspeccapybara

提问by tomekfranek

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 withinI get this error:

没有within我得到这个错误:

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

回答by Andrei Botalov

You can just use:

你可以只使用:

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

or

或者

first('.item > a').click

(if your default selector is :css)

(如果您的默认选择器是 :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 找到了几个.item's 所以它引发了一个异常。我认为 Capybara 2 的这种行为非常好。

回答by Elle Mundy

This phrasing also works:

这句话也适用:

within first(".item") do
  click_link "Agree"
end

回答by DGM

Xpath can address the element. I'm not very good with it yet, but something like //div[@class='active'][1]/a

Xpath 可以寻址元素。我还不是很擅长,但类似//div[@class='active'][1]/a

That may or may not work, but the point is that xpath can address an array of matches and pull out a particular one. You should be able to match with this.

这可能有效,也可能无效,但关键是 xpath 可以寻址一组匹配项并提取特定的匹配项。你应该能够匹配这个。

A working example example from one of my projects:

我的一个项目中的一个工作示例:

within page.find("div.panel", text: /Proposals/) do
  within page.find('tr', text: /Foo/) do
    page.should have_xpath('td[3]', text: @today)
  end
end

回答by nroose

Since first() doesn't always wait, perhaps this is useful:

由于 first() 并不总是等待,也许这很有用:

expect(page).to have_css("selector")                               
first("selector").click

回答by Salomanuel

most of those solutions will not use Capybara's brilliant waiting features

大多数这些解决方案不会使用 Capybara 出色的等待功能

better do as this link suggests:
https://thoughtbot.com/blog/write-reliable-asynchronous-integration-tests-with-capybara#find-the-first-matching-element

最好按照这个链接的建议去做:https:
//thoughtbot.com/blog/write-reliable-asynchronous-integration-tests-with-capybara#find-the-first-matching-element

Bad:

坏的:

first(".active").click
If there isn't an .active element on the page yet, first will return nil and the click will fail.

first(".active").click
如果页面上还没有 .active 元素, first 将返回 nil 并且点击将失败。

Good:

好的:

If you want to make sure there's exactly one
find(".active").click

如果你想确保只有一个
find(".active").click

If you just want the first element
find(".active", match: :first).click
Capybara will wait for the element to appear before trying to click.

如果您只想要第一个元素,
find(".active", match: :first).click
Capybara 将在尝试单击之前等待该元素出现。

Note that match: :firstis more brittle, because it will silently click on a different element if you introduce new elements which match.

请注意,match: :first它更脆弱,因为如果您引入匹配的新元素,它会默默地点击不同的元素。

回答by IT Vlogs

Simple you can use:

简单你可以使用:

$('.item').find('a').first().click();