水豚如何获取父节点?
我正在使用许多 jQuery 插件,这些插件通常会创建没有 id 或其他标识属性的 DOM 元素,而在 Capybara 中获取它们的唯一方法(例如单击) - 是首先获取它们的邻居(其祖先的另一个子元素) 。但我没有找到任何地方,Capybara是否支持这样的东西,例如:
find('#some_button').parent.fill_in "Name:", :with => name
?
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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我确实发现 jamuraa 的答案很有帮助,但是在我的情况下,使用完整的 xpath 给我带来了字符串的噩梦,所以我很高兴地利用了 Capybara 中连接 find 的功能,允许我混合 css 和 xpath 选择。您的示例将如下所示:
Capybara 2.0 update:
find(:xpath,".//..")
很可能会导致不明确的匹配错误。在这种情况下,请改用
first(: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:
Capybara 2.0 update:
find(:xpath,".//..")
will most likely result in anAmbiguous match
error. In that case, usefirst(:xpath,".//..")
instead.我发现以下内容确实有效:
Capybara 已更新以支持此功能。
https://github.com/jnicklas/capybara/pull/505
I found the following that does work:
Capybara has been updated to support this.
https://github.com/jnicklas/capybara/pull/505
水豚和 CSS 没有办法做到这一点。不过,我过去曾使用 XPath 来实现这个目标,它确实有办法获取父元素,并且受到 Capybara 的支持:
There isn't a way to do this with capybara and CSS. I've used XPath in the past to accomplish this goal though, which does have a way to get the parent element and is supported by Capybara:
如果您在试图弄清楚如何找到任何父节点(如祖先)节点时偶然发现了这一点(如@vrish88对@Pascal Lindelauf答案的评论中所暗示的那样):
If you stumbled across this trying to figure out how to find any parent (as in ancestor) node (as hinted at in @vrish88's comment on @Pascal Lindelauf's answer):
这个答案涉及如何操作同级元素,我相信原始问题所暗示的就是这个
您的问题假设只需稍作调整即可。如果动态生成的字段看起来像这样并且没有 id: 那么
您的查询将是:
如果动态生成的输入确实附加了 id 元素(请小心这些元素,尽管在角度中,它们不会根据添加和删除元素)它会是这样的:
希望有帮助。
This answer pertains to how to manipulate a sibling element which is what I believe the original question is alluding to
Your question hypothesis works with a minor tweak. If the dynamically generated field looks like this and does not have an id:
Your query would then be:
If the dynamically generated input does come attached with an id element (be careful with these though as in angular, they are wont to change based on adding and deleting elements) it would be something like this:
Hope that helps.
正如@Tyler Rick Capybara 在评论中提到的,这些天有方法[
祖先(选择器)
和兄弟(选择器)
As mentioned in comment by @Tyler Rick Capybara in these days have methods[
ancestor(selector)
andsibling(selector)
我使用不同的方法,首先使用父元素中的文本查找父元素:
也许这在类似的情况下很有用。
I'm using a different approach by finding the parent element first using the text within this parent element:
Maybe this is useful in a similiar situation.
我需要找到具有 css 类的祖先,尽管目标祖先是否存在一个或多个 css 类是不确定的,所以我没有找到一种进行确定性 xpath 查询的方法。我做了这个:
警告:谨慎使用它,它可能会在测试中花费你很多时间,所以确保祖先离你只有几跳远。
I needed to find an ancestor with a css class, though it was indeterminate if it the target ancestor had one or more css classes present, so I didn't see a way to make a deterministic xpath query. I worked this up instead:
Warning: use this sparingly, it could cost you a lot of time in tests so make sure the ancestor is just a few hops away.
这是
http://rubydoc.info/github/jnicklas/ capybara/master/Capybara/Node/Base:parent
有一个父方法;)
Here it is
http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Base:parent
There is a parent method present;)