问题描述
我正在使用许多 jQuery 插件,这些插件通常创建没有 id 或其他标识属性的 DOM 元素,并且在 Capybara 中获取它们的唯一方法(例如单击) - 是获取它们的邻居(它的另一个孩子先祖)先.但是我没有找到任何地方,例如Capybara是否支持这样的事情:
I'm working with many jQuery plugins, that often create DOM elements without id or other identification properties, and the only way to get them in Capybara (for clicking for example) - is to get their neighbor (another child of its ancestor) first. But I didn't find anywhere, does Capybara support such things for example:
find('#some_button').parent.fill_in "Name:", :with => name
?
推荐答案
我真的发现 jamuraa 的回答很有帮助,但是在我的情况下,使用完整的 xpath 给了我一个字符串的噩梦,所以我很高兴地利用了连接的能力在 Capybara 中找到,允许我混合 css 和 xpath 选择.您的示例将如下所示:
I really found jamuraa's answer helpful, but going for full xpath gave me a nightmare of a string in my case, so I happily made use of the ability to concatenate find's in Capybara, allowing me to mix css and xpath selection. Your example would then look like this:
find('#some_button').find(:xpath,".//..").fill_in "Name:", :with => name
Capybara 2.0 更新:find(:xpath,".//..")
很可能会导致 Ambiguous match
错误.在这种情况下,请改用 first(:xpath,".//..")
.
Capybara 2.0 update: find(:xpath,".//..")
will most likely result in an Ambiguous match
error. In that case, use first(:xpath,".//..")
instead.
这篇关于如何在 Capybara 中获取父节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!