XPath: Difference between revisions

From GreaseSpot Wiki
Jump to navigationJump to search
m (→‎Extensions: added links to wiki page: Useful_Tools_for_Script_Writers)
(→‎Extensions: Remove Blatant POV)
Line 19: Line 19:
You can quickly determine the XPath to an element with the [https://addons.mozilla.org/en-US/firefox/addon/1095 XPath Checker] addon. Just right-click an element and select "XPath". A window will pop up with the expression. You can also experiment with new expressions interactively.
You can quickly determine the XPath to an element with the [https://addons.mozilla.org/en-US/firefox/addon/1095 XPath Checker] addon. Just right-click an element and select "XPath". A window will pop up with the expression. You can also experiment with new expressions interactively.


A better extension is [[Useful_Tools_for_Script_Writers#XPather | XPather]].
Another extension is [[Useful_Tools_for_Script_Writers#XPather | XPather]], which relies on [[Useful_Tools_for_Script_Writers#DOM_Inspector | DOM Inspector]].


In [[Useful_Tools_for_Script_Writers#FireBug|FireBug]], you can right-click a node in the HTML inspector and select "Copy XPath". A <code>$x</code> function (like the XPath helper function linked above but with no support for specifying a context node) is available in the Firebug console.
In [[Useful_Tools_for_Script_Writers#FireBug|FireBug]], you can right-click a node in the HTML inspector and select "Copy XPath". A <code>$x</code> function (like the XPath helper function linked above but with no support for specifying a context node) is available in the Firebug console.

Revision as of 21:00, 29 December 2008


XPath (XML Path Language) is a language for addressing elements in a XML or HTML document. XPath expressions describe paths in a tree represention of the document.

XPath is very fast compared to manual DOM traversal (see e.g. getElementsByClassName Speed Comparison).

This page is intended for tips and gotchas related to using XPath in Greasemonkey scripts, not complete documentation. See the links below for that.

Using XPath in Greasemonkey

The most convenient way to use XPath in Greasemonkey scripts is with a helper function. The insides of that function illustrate the less convenient way.

Relative paths

Note that relative XPath expressions, that start from some specific node, do not begin with a /. Start them with a period, e.g. .//a, or an axis, e.g. descendant::a. The default axis is child, so just a works to find an immediate descendant.

Extensions

You can quickly determine the XPath to an element with the XPath Checker addon. Just right-click an element and select "XPath". A window will pop up with the expression. You can also experiment with new expressions interactively.

Another extension is XPather, which relies on DOM Inspector.

In FireBug, you can right-click a node in the HTML inspector and select "Copy XPath". A $x function (like the XPath helper function linked above but with no support for specifying a context node) is available in the Firebug console.

Links