【问题标题】:Iterate through all form fields with selenium in ruby用红宝石中的硒遍历所有表单字段
【发布时间】:2011-03-14 21:56:19
【问题描述】:

我正在尝试使用 Selenium 验证我的表单字段是否都具有关联的标签,但在抓取页面上的所有表单字段时遇到问题。 get_all_fields 只获取文本字段;我无法同时获取密码、收音机、复选框等。

我正在尝试这样的事情:

num_fields = Integer(selenium.get_xpath_count("//input"))

1.upto(num_fields) do |field_number|
  input_id = selenium.get_attribute("//input[#{field_number}]@id")

  selenium.element?("css=label[for=#{input_id}]")

end

问题是 //input[1] 不起作用;根据页面,输入嵌套在各种标记中。

有没有办法使用 selenium 定位器来一般抓取第一个、第二个等输入?

【问题讨论】:

    标签: ruby testing selenium


    【解决方案1】:

    尝试使用//body/descendant::input[#{field_number}]

    【讨论】:

      【解决方案2】:

      稍微了解一下 XPath 的工作原理将有助于进行此类测试; Dave Hunt 对//body/descendant::input[#{field_number}] 的建议还不错; descendant::input 部分将返回 field_number 将索引到的数组。

      还有其他 XPath 轴也可以提供数组 - 您可能还想使用表单元素作为后代元素的开始而不是正文。

      唯一的缺点是每次循环都会对 xpath 表达式进行评估。如果您有很多输入控件,或者像 IE 这样的慢速浏览器,这可能需要相对较长的时间——特别是如果您有其他标准检查要做。您最好使用selenium.get_html_source 并开发一个WebStandardsChecker 类来一次性评估页面。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-04-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-09
        • 1970-01-01
        • 1970-01-01
        • 2019-04-27
        相关资源
        最近更新 更多