Read more

Capybara: Easiest way to get the parent of an element

Deleted user #4117
September 22, 2017Software engineer

If you already selected an element and want to get its parent, you can call find(:xpath, '..') on it.
To get the parents parent, call find(:xpath, '../..').

Example

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

Find a link which contains a twitter icon and check that it links to the correct page:

<a href="http://twitter.com/">
  <i class="icon is-twitter"></i>
</a>
link = page.find("a .icon.is-twitter").find(:xpath, '..')
link[:href].should == "http://twitter.com/"

There is a good overview on xpath syntax on w3schools Show archive.org snapshot . But as xpath expressions can get quite complex and hard to understand for fellow developers, it's best to keep them short and simple.

Posted to makandra dev (2017-09-22 14:09)