简体   繁体   English

使用红宝石中的硒迭代所有表单字段

[英]Iterate through all form fields with selenium in ruby

I am trying to validate that my form fields all have an associated label using Selenium, but I am having problems grabbing all of the form fields on a page. 我正在尝试使用Selenium验证我的表单字段都有一个关联的标签,但我在抓取页面上的所有表单字段时遇到问题。 get_all_fields only gets textfields; get_all_fields只获取文本字段; I have no way to also grab the passwords, radios, checkboxes, etc. 我也无法获取密码,收音机,复选框等。

I was trying something like this: 我正在尝试这样的事情:

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

The problem is that //input[1] doesn't work; 问题是//输入[1]不起作用; the inputs are nested in various markup depending on the page. 输入根据页面嵌套在各种标记中。

Is there a way to use a selenium locator to generically grab the first, second, etc input? 有没有办法使用selenium定位器来一般地抓取第一个,第二个等输入?

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

Learning a bit about how XPath works will help with this sort of testing; 了解XPath的工作原理将有助于进行此类测试; Dave Hunt's suggestion of //body/descendant::input[#{field_number}] is pretty good; Dave Hunt关于//body/descendant::input[#{field_number}]的建议相当不错; the descendant::input part will return an array that the field_number will index into. descendant::input部分将返回field_number将索引的数组。

There are other XPath axes that will also give arrays - you may also want to use the form element as the start of the descendants rather than body. 还有其他XPath轴也会给出数组 - 您可能还希望使用form元素作为后代的开头而不是body。

The only downside is that the xpath expression will get evaluated each time around the loop. 唯一的缺点是每次循环时都会评估xpath表达式。 If you have a lot of input controls, or a slow browser like IE, this may take a comparatively long time - especially if you have other standard checks to make. 如果您有很多输入控件或像IE这样的慢速浏览器,这可能需要相当长的时间 - 特别是如果您要进行其他标准检查。 You may be better off using selenium.get_html_source and developing a WebStandardsChecker class to evaluate the page in a single pass. 您可能最好使用selenium.get_html_source并开发WebStandardsChecker类来WebStandardsChecker评估页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM