迭代Capybara中的物品

Mic*_*ael 15 iteration loops capybara

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

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

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
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

Jus*_* Ko 42

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

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

page.all(:css, '.block').each do |el|
    puts el.text
end
Run Code Online (Sandbox Code Playgroud)

page.all返回匹配元素的数组.所以如果你只想要第二个匹配元素,你可以这样做:

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