Iterating through items in Capybara

后端 未结 1 1256
长情又很酷
长情又很酷 2021-02-04 05:37

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

相关标签:
1条回答
  • 2021-02-04 06:10

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

    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 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
    
    0 讨论(0)
提交回复
热议问题