Testing XPath in Google Chrome and Firefox

Published 11 July 2012 under how to, xpath

If you ever use tools such as Nokogiri or Selenium trying to come up with the right XPath can be tricky. Wouldn't it be great if we could try out an XPath right in the browser? With Google Chrome and Firefox you can!

In Google Chrome this is built right in - no need to install any extensions for this to work. To try out an Xpath, open up the Javascript console (Alt-Cmd-j in Mac OS X) in Google Chrome. Firefox users will need Firebug to get similar functionality.

Now we can use $x to try out our lovely XPath:

> $x("//div[@class='date']")
  [<div class="date">...</div>]

> $x("//a[contains(@href, 'news')]")
  [<a href="/news">News</a>]


blog comments powered by Disqus