2014-09-02 148 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