代码之家  ›  专栏  ›  技术社区  ›  AME

通过类元素的Watir循环

  •  0
  • AME  · 技术社区  · 10 年前

    我如何循环浏览所有元素?

    我要找的是:

    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
    

    感谢您抽出时间。

    2 回复  |  直到 10 年前
        1
  •  2
  •   MichaelR    10 年前

    对于您所描述的内容,只需使用 :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
    

    或循环使用属性的所有text_fields :name => "asdf" :

    browser.text_fields(:name => "asdf").each { |elem| elem.click }
    
        2
  •  1
  •   Justin Ko    10 年前

    要循环浏览所有匹配的元素,您需要查找 "element collections" .

    基本上,您需要将用于获取元素的方法多元化,然后可以使用 [] 要获取特定索引:

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

    元素集合包括Enumerable,因此也有多种迭代方法。

    在文档方面,您可以研究: