While most developers are familiar with CSS selectors, this is not always the most efficient mechanism for referencing a particular element in a page.
XPath is a technology that is often overlooked by developers because of the perception that all things XML are overly complex. That said, XPath selectors can be a very powerful and flexible approach for finding an element.
For example, given a simple DOM structure like this:
<dl> <dt id="hw">Hello, world</dt> <dd>A common greeting</dd> </dl>
It is possible to find elements in all sorts of different ways:
- Find the
dtelement by its text content:
- Find the parent of the element with
- Find the preceding
dtelement of the
These are just some very simple examples of the sorts of complex element selection you can only perform using XPath. The next time you need to find an element with no simple identifier, an XPath selector could be the solution.