How to get parent node in Capybara? How to get parent node in Capybara? ruby-on-rails ruby-on-rails

How to get parent node in Capybara?


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.


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


I found the following that does work:

find(:xpath, '..')

Capybara has been updated to support this.

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