// XPath CheatSheet // 1. General. '/html' // whole web page (css: html) '/html/body' // whole web page body (css: body) '//text()' // all text nodes of web page '/html/body/.../.../.../E' // element by absolute reference (css: body > … > … > … > E) // 2. Tag. '//E' // element by relative reference (css: E) '(//E)[2]' // second element anywhere on page '//img' // image element (css: img) '//E[@A]' // element with attribute A (css: E[A]) '//E[@A="t"]' // element with attribute A containing text 't' exactly (css: E[A='t']) '//E[contains(@A,"t")]' // element with attribute A containing text 't' (css: E[A*='t']) '//E[starts-with(@A, "t")]' // element whose attribute A begins with 't' (css: E[A^='t']) '//E[ends-with(@A, "t")]' // element whose attribute A ends with 't' (css: E[A$='t']) '//E[contains(concat(" ", @A, " "), " w ")' // element with attribute A containing word 'w' (css: E[A~='w']) '//E[matches(@A, "r")]' // element with attribute A matching regex ‘r’ '//E1[@id=I1] | //E2[@id=I2]' // element with id I1 or element with id I2 (css: E1#I1, E2#I2) '//E1[@id=I1 or @id=I2]' // element with id I1 or id I2 (css: E1#I1, E1#I2) // 3. Attribute. '//E/@A' // attribute A of element (css: E@A) '//*/@A' // attribute A of any element (css: *@A) '//E[@A2="t"]/@A1' // attribute A1 of element where attribute A2 is 't' exactly (css: E[A2='t']@A1) '//E[contains(@A,"t")]/@A' // attribute A of element where A contains 't' (css: E[A*='t']@A) // 4. ID & Name. '//*[@id="I"]' // element with id I (css: #I) '//E[@id="I"]' // element with id I (css: E#I) '//*[@name="N"]' // element with name (css: [name='N']) '//E[@name="N"]' // element with name (css: E[name='N']) '//*[@id="X" or @name="X"]' // element with id X or, failing that, a name X '//*[@name="N"][v+1]' // element with name N & specified 0-based index ‘v’ (css: [name='N']:nth-child(v+1)) '//*[@name="N"][@value="v"]' // element with name N & specified value ‘v’ (css: *[name='N'][value='v’]) // 5. Lang & Class. '//E[@lang="L" or starts-with(@lang, concat("L", "-"))]' // element is explicitly in language L or subcode (css: E[lang|=L]) '//*[contains(concat(" ", @class, " "), " C ")]' // element with a class C (css: .C) '//E[contains(concat(" ", @class, " "), " C ")]' // element with a class C (css: E.C) // 6. Text & Link. '//*[.="t"]' // element containing text 't' exactly '//E[contains(text(), "t")]' // element containing text 't' (css: E:contains('t')) '//a' // link element (css: a) '//a[.="t"]' // element containing text 't' exactly '//a[contains(text(), "t")]' // element containing text 't' (css: a:contains('t')) '//a[@href="url"]' // with target link 'url' (css: a[href='url']) '//a[.="t"]/@href' // link URL labeled with text 't' exactly // 7. Parent & Child. '//E/*[1]' // first child of element (css: E > *:first-child) '//E[1]' // first child (css: E:first-of-type) '//E/*[last()]' // last child of element E (css: E *:last-child) '//E[last()]' // last child (css: E:last-of-type) '//E[2]' // second child (css: E:nth-of-type(2)) '//*[2][name()="E"]' // second child that is an element (css: E:nth-child(2)) '//E[last()-1]' // second-to-last child (css: E:nth-last-of-type(2)) '//*[last()-1][name()="E"]' // second-to-last child that is an element (css: E:nth-last-child(2)) '//E1/[E2 and not( *[not(self::E2)])]' // element with only children '//E/..' // parent of element '//*[@id="I"]/.../.../.../E' // descendant of element with id I using specific path (css: #I > … > … > … > E) '//*[@id="I"]//E' // descendant of element with id I using unspecified path (css: #I E) '//E[count(*)=0]' // element with no children (E:empty) '//E[count(*)=1]' // element with an only child '//E[count(preceding-sibling::*)+count(following-sibling::*)=0]' // Element that is an only child (css: E:only-child) '//E[count(../E) = 1]' // Element with no siblings (css: E:only-of-type) '//E[position() mod N = M + 1]' // Every Nth element starting with the (M+1)th (css: E:nth-child(Nn+M)) // 8. Sibling. '//E2/following-sibling::E1' // Element following some sibling (css: E2 ~ E1) '//E2/following-sibling::*[1][name()="E1"]' // Element immediately following sibling (css: E2 + E1) '//E2/following-sibling::*[2][name()="E1"]' // Element following sibling with one intermediary (css: E2 + * + E1) '//E/following-sibling::*' // Sibling element immediately following (css: E + *) '//E2/preceding-sibling::E1' // Element preceding some sibling '//E2/preceding-sibling::*[1][name()="E1"]' // Element immediately preceding sibling '//E2/preceding-sibling::*[2][name()="E1"]' // Element preceding sibling with one intermediary '//E/preceding-sibling::*[1]' // Sibling element immediately preceding // 9. Table Cell.