XPath Cookbook: Difference between revisions

From Littledamien Wiki
Jump to navigation Jump to search
(Created page with "== Search for elements == === Top-level element === Use a single slash: `/body` === Element anywhere within DOM === Use two slashes: `//a` == Search the children of an...")
 
(No difference)

Latest revision as of 16:18, 27 August 2019

Search for elements[edit]

Top-level element[edit]

Use a single slash:

/body

Element anywhere within DOM[edit]

Use two slashes:

//a

Search the children of an element[edit]

Search top-level children of an element with single slash:

//div/a

Search all children at any level under the parent element with double slashes:

This will return all children at any level below the parent element.

//div//a

Search for an element based on the value of one of its attributes[edit]

Exact value of the attribute[edit]

//div[@class="foo"]

Partial match of the attribute value[edit]

//div[contains(@class, "foo")]

Partial match of multiple possible attribute values[edit]

//div[contains(@class, "foo") | contains(@class, "bar")]