Search This Blog

Wednesday, August 4, 2010

Examples that you could try out using XPath Checker

Here are some examples that you could try out using XPath Checker:
Get the title of a page:
//title/text()
List all the images on a page:
//img
List the images that are inside a link:
//a//img
List the images that have alt tags:
//img[@alt]
List the images that don't have alt tags:
//img[not(@alt)]
Show all the alt tags:
//img/@alt
Show the href for every link:
//a/@href
Get an element with a particular CSS id:
//*[@id='mainContent']
To install XPath Checker, you need at least version 1.0.3 of Firefox. Once you have Firefox running, you can download XPath Checker from addons.mozilla.org and it will automatically install. Then you need to restart Firefox.
To use XPath Checker, right-click (or control-click if you have a Mac) anywhere on any web page and choose "View XPath" in the context menu. An editor window will pop up. The editor will show an XPath pointing to whatever you clicked on appear at the top of the window and the results of executing that XPath underneath. You can edit the XPath and the results automatically update as you type.
For developers: I'm releasing XPath Checker under the GPL. Here's the source code. If you get stuck, send me email and maybe I'll write some documentation.