2014-09-02 120 views
0

如何循环遍历所有元素?Watir循环通过类元素

我在寻找类似:

brower.text_field[0](:name, "asdf").click # get the first element 
brower.text_field[1](:name, "asdf").click # get the second element 

是否有更先进的东西有一个好的文档? 我还没有发现任何有用的东西我的一切很简单的东西,但我正在寻找的东西,我可以像链要素:

browser.tr(:id, "asdf").td.click 

谢谢您的时间。

回答

1

要循环访问所有匹配的元素,您正在寻找"element collections"

基本上你需要变复数用于获取元素的方法,然后你可以使用[]得到一个特定的索引:

brower.text_fields(:name, "asdf")[0].click # get the first element 
brower.text_fields(:name, "asdf")[1].click # get the second element 

元素集合包括枚举的,所以也有多种的方法迭代。

在文档方面,你可以看看:

+0

感谢您的详细解释。 – 2014-09-02 13:06:03

2

对于您所描述的内容,您可以简单地使用:index属性:

brower.text_field(:name => "asdf", :index => 0).click # get the first element 
brower.text_field(:name => "asdf", :index => 1).click # get the second element 

或者遍历与属性:name => "asdf"所有text_fields:

browser.text_fields(:name => "asdf").each { |elem| elem.click } 
+0

+1为您的时间。谢谢 – 2014-09-02 13:05:05