Adding support for getting an element's parent #505

wants to merge 1 commit into


None yet

10 participants

andyw8 commented Sep 24, 2011

It seemed Capybara lacked a simple way to get an element's parent ( ) so I've added support for this.


Just do find(:xpath, '..'). No need to expose these kinds of traversal methods in Capybara.

@jnicklas jnicklas closed this Nov 15, 2011
cj commented Mar 7, 2012

@jnicklas is there a way to do that but lazy find the body? I'm trying to get back to the body from inside a within, so that I can check it loaded a popup....

cj commented Mar 7, 2012

@jnicklas looks like find(:xpath, '//body') works :D

dgm commented Jun 24, 2012

How is this not a useful addition? Not everyone is comfortable with xpath... I'd prefer to have a jquery like navigation available. Even more useful would be something like

Sorry for dragging an old issue up.

I don't agree with jnicklas. capybaras purpose is to allow us to test against a page, which may include finding an element then traversing to another element to check something. It may be slightly less common than click "somebutton" but its still valuable.

dfl commented Oct 9, 2012

+1 I would like this feature too, even if it's just syntactic sugar

dfl commented on f0b88ea Oct 9, 2012

this doesn't work if you chain it... seems like you can't call subsequent finds on the returned node.
NoMethodError: undefined method `[]' for #Capybara::Document


When the hell did we merge this? This is definitely, definitely not the right way to implement that method, no idea how that got in there.


I don't think we ever merged it. It's part of #505, and GitHub's UI is just confusing.

jnicklas commented Oct 9, 2012

I'm not against this feature, though it is just syntactic sugar, so I find it a bit redundant. This pull request though is just completely wrong. So if someone wants this, please send a better pull request.

janko-m commented Apr 7, 2015

What does Capybara::Node::Base#parent attribute currently do? It seems to be always returning Capybara::Document instead of the actual tag's parent.

twalpole commented Apr 7, 2015

@janko-m Its the context for the find which located the current element.

abotalov commented Apr 7, 2015

@janko-m Base#parent is internally used to get an ancestor using which the current element was located. For example in:

el = find('a')
el.find('b').parent # => returns el
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment