Ruby-on-rails 水豚:查找(元素)使用选择器来定位复杂的属性名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7667501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
capybara: find(element) using selector to target a complex attribute name
提问by Pierre
Using cucumber and capybara to test a rails app. Assuming I cannot change the markup, can I use capybara to select the following select in a page full of similar tds and selects?
使用黄瓜和水豚测试 Rails 应用程序。假设我无法更改标记,我可以使用水豚在充满相似tds 和selects的页面中选择以下选择吗?
<td>
<select name="attributes[ruby][category]">
<option value="2" selected="selected">Languages</option>
<option value="3">Communication</option>
</select>
</td>
This seems to fail (I assume because of the nested "[" and "]").
这似乎失败了(我假设是因为嵌套的“[”和“]”)。
find("select[name=attributes[ruby][category]]")
Escaping doesn't work either. Thoughts?
逃避也没有用。想法?
回答by maro
You can try find('select', :name => 'attributes[ruby][category]')or find_field('attributes[ruby][category]').
你可以试试find('select', :name => 'attributes[ruby][category]')或find_field('attributes[ruby][category]')。
回答by Andy Waite
I think you need to quote the attribute value:
我认为您需要引用属性值:
find("select[name='attributes[ruby][category]']")
but maro's suggestion of using find_field is a cleaner approach.
但是 maro 建议使用 find_field 是一种更简洁的方法。
回答by Baruch
More generally you can use XPath
更一般地,您可以使用 XPath
find(:xpath, "//select[@name='attributes[ruby][category]'")
This approach has the advantage that it can be used for any attribute.
这种方法的优点是它可以用于任何属性。

