遍历水豚中的物品 [英] Iterating through items in Capybara

查看:79
本文介绍了遍历水豚中的物品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面,其中包含.block类的多个元素.在Capybara中,我希望能够在完成操作之前遍历并引用此类中的每个元素.

I've got a page containing multiple elements of class .block. In Capybara, I want to be able to loop through and refer to each of the elements with this class before completing an action.

但是,到目前为止,我尝试过的所有代码都没有起作用.这是我尝试过的:

However, none of the code I've tried so far has worked. Here's what I've tried:

within('.block:nth-child(1)') do
  find('.Button').click
end

page.find('.block').all.first.find('Button').click

page.find('.block').all[1].find('Button').click

有什么想法吗?

推荐答案

您要使用all方法(请参见http://rubydoc.info/github/jnicklas/capybara/Capybara/Node/Finders#all-instance_method ).

You want to use the all method (see http://rubydoc.info/github/jnicklas/capybara/Capybara/Node/Finders#all-instance_method).

使用"block"类输出每个元素的文本(即迭代)的示例将是:

An example of outputting the text of each element (ie iterating) with class 'block' would be:

page.all(:css, '.block').each do |el|
    puts el.text
end

page.all返回匹配元素的数组.因此,如果您只想要第二个匹配元素,则可以执行以下操作:

page.all returns an array of matching elements. So if you just want the second matching element, you can do:

page.all(:css, '.block')[1]  #Note that it is 0-based index

这篇关于遍历水豚中的物品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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