Skip to content

Instantly share code, notes, and snippets.

@chrisdarroch
Created July 14, 2010 01:36
Show Gist options
  • Save chrisdarroch/474870 to your computer and use it in GitHub Desktop.
Save chrisdarroch/474870 to your computer and use it in GitHub Desktop.
// You can try running these in FireBug console on the above html file.
$x("//li[@id='two']//preceding::li")
// => [ li#first, li#second, li#third, li#one ]
$x("//li[@id='two']//preceding-sibling::li")
// => [ li#one ]
$x("//ul[@class='outside']/li[1]//preceding::li")
// => [li#first, li#second, li#third, li#fourth, li#one, li#two, li#three, li#fifth]
$x("//ul[@class='outside']/preceding::li[1]")
// => [li#fifth]
<html>
<body>
<ol>
<li id="first">Number one</li>
<li id="second">Number two</li>
<li id="third">Third time's a charm</li>
<li id="fourth">
<ul>
<li id="one">Sub one</li>
<li id="two">Second sub</li>
<li id="three">Third and final sub</li>
</ul>
</li>
<li id="fifth">One more for good measure</li>
</ol>
<ul class="outside">
<li>Another list item.</li>
<li>Just one more...</li>
</ul>
</body>
</html>
# Running a custom XPath in capybara involves the following
# (assuming you're in a step_definition file, thus your execution context is Cucumber::Rails::World
page.has_xpath?('//xpath/to/element')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment