水豚如何获取父节点?

发布于 2024-10-15 09:26:04 字数 242 浏览 4 评论 0原文

我正在使用许多 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

﹏雨一样淡蓝的深情 2024-10-22 09:26:04

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

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

Capybara 2.0 updatefind(: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:

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

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

薆情海 2024-10-22 09:26:04

我发现以下内容确实有效:

find(:xpath, '..')

Capybara 已更新以支持此功能。

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

I found the following that does work:

find(:xpath, '..')

Capybara has been updated to support this.

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

╰ゝ天使的微笑 2024-10-22 09:26:04

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

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

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:

find(:xpath, '//*[@id="some_button"]/..').fill_in "Name:", :with => name
薄情伤 2024-10-22 09:26:04

如果您在试图弄清楚如何找到任何父节点(如祖先)节点时偶然发现了这一点(如@vrish88对@Pascal Lindelauf答案的评论中所暗示的那样):

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

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

find('#some_button').find(:xpath, 'ancestor::div[@id="some_div_id"]')
破晓 2024-10-22 09:26:04

这个答案涉及如何操作同级元素,我相信原始问题所暗示的就是这个

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

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

您的查询将是:

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

如果动态生成的输入确实附加了 id 元素(请小心这些元素,尽管在角度中,它们不会根据添加和删​​除元素)它会是这样的:

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

希望有帮助。

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:

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

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

Hope that helps.

大海や 2024-10-22 09:26:04

正如@Tyler Rick Capybara 在评论中提到的,这些天有方法[
祖先(选择器)兄弟(选择器)

As mentioned in comment by @Tyler Rick Capybara in these days have methods[
ancestor(selector) and sibling(selector)

撩发小公举 2024-10-22 09:26:04

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

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

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

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.

ゞ花落谁相伴 2024-10-22 09:26:04

我需要找到具有 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

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

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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文