Ruby-on-rails 如何在 Capybara 中获取父节点?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4861863/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:15:54  来源:igfitidea点击:

How to get parent node in Capybara?

ruby-on-railsrubycucumberbddcapybara

提问by sandrew

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:

我正在使用许多 jQuery 插件,这些插件通常会创建没有 id 或其他标识属性的 DOM 元素,并且在 Capybara 中获取它们的唯一方法(例如单击) - 是首先获取它们的邻居(其祖先的另一个孩子) . 但是我没有找到任何地方,Capybara 是否支持这样的事情,例如:

find('#some_button').parent.fill_in "Name:", :with => name

?

?

回答by Pascal Lindelauf

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:

我真的发现 jamuraa 的回答很有帮助,但是在我的情况下,使用完整的 xpath 给了我一个字符串的噩梦,所以我很高兴地利用了在 Capybara 中连接 find 的能力,允许我混合使用 css 和 xpath 选择。您的示例将如下所示:

find('#some_button').find(:xpath,".//..").fill_in "Name:", :with => name

Capybara 2.0 update: find(:xpath,".//..")will most likely result in an Ambiguous matcherror. In that case, use first(:xpath,".//..")instead.

Capybara 2.0 更新find(:xpath,".//..")很可能会导致Ambiguous match错误。在这种情况下,请first(:xpath,".//..")改用。

回答by jamuraa

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:

没有办法用水豚和 CSS 做到这一点。不过,我过去曾使用 XPath 来实现此目标,它确实有一种获取父元素的方法,并且由 Capybara 支持:

find(:xpath, '//*[@id="some_button"]/..').fill_in "Name:", :with => name

回答by B Seven

I found the following that does work:

我发现以下确实有效:

find(:xpath, '..')

Capybara has been updated to support this.

Capybara 已更新以支持此功能。

https://github.com/jnicklas/capybara/pull/505

https://github.com/jnicklas/capybara/pull/505

回答by Ben Alavi

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):

如果您偶然发现了这个试图找出如何找到任何父节点(如在祖先中)节点(如@vrish88 对@Pascal Lindelauf 的回答的评论中所暗示的):

find('#some_button').find(:xpath, 'ancestor::div[@id="some_div_id"]')

回答by Harm de Wit

I'm using a different approach by finding the parent element first using the text within this parent element:

我正在使用不同的方法,首先使用此父元素中的文本查找父元素:

find("<parent element>", text: "text within your #some_button").fill_in "Name:", with: name

Maybe this is useful in a similiar situation.

也许这在类似的情况下很有用。

回答by T. Slater

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:

您的问题假设只需稍作调整即可。如果动态生成的字段看起来像这样并且没有 id:

<div>
  <input></input>
  <button>Test</button>
</div>

Your query would then be:

您的查询将是:

find('button', text: 'Test').find(:xpath, "..").find('input').set('whatever')

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:

如果动态生成的输入确实附带了一个 id 元素(尽管在 angular 中要小心这些,但它们不会根据添加和删除元素而改变),它会是这样的:

find('button', text: 'Test').find(:xpath, "..").fill_in('#input_1', with: 'whatever')

Hope that helps.

希望有帮助。

回答by kross

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:

我需要找到一个具有 css 类的祖先,但不确定目标祖先是否存在一个或多个 css 类,所以我没有看到进行确定性 xpath 查询的方法。我解决了这个问题:

def find_ancestor_with_class(field, cssClass)
  ancestor = field
  loop do
    ancestor = ancestor.find(:xpath, '..')
    break if ancestor.nil?

    break if ancestor.has_css? cssClass
  end

  ancestor
end

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.

警告:谨慎使用它,它可能会在测试中花费你很多时间,所以请确保祖先离它只有几步之遥。