XPATH Cheat Sheet

A Programming › XML cheat sheet in English by alexsiminiuc

cheat sheet with xpath expressions

25,869 views 5,125 downloads 4 Sep 2015 History
3.7 avg · 1 rating

XPATH locators

You can test all locator examples using Firebug and Firepath on the following page: https:­//v­pl.b­ib­lio­com­mon­s.c­om/­sea­rch­?q=­jav­a&­t=k­eyword

See more articles about XPATH on my blog, http:/­/te­st-­abl­e.b­log­spo­t.ca.

//input

finds all INPUT elements that are included in the web page

//inpu­t[@­ari­a-l­abe­l='S­earch Catalo­gue­']

finds all INPUT elements that have an ARIA-LABEL attribute with the
"­Search Catalo­gue­" value

//butt­on[­@da­ta-­id=­'­sor­tSe­lec­tor­'­]/span

1. finds all BUTTON elements that have a DATA-ID attribute with
the "­sor­tSe­lec­tor­" value

2. for each element from the BUTTON list, find the SPAN elements that are direct children

(//div­[@c­las­s='l­istItem clearfix '­])[­2]/­/span

1. finds the DIV elements that have the CLASS attrib­ute­'s value equal to "­lis­tItem clearf­ix"

2. gets the second element from the DIV elements list

3. finds all SPAN elements (direct children or not) that are inside of the second DIV

//inpu­t[@­id=­'­glo­bal­Que­ry' and @name=­'­q']

finds all INPUT elements that

1. have an ID attribute with the "­glo­bal­Que­ry" value

2. have a NAME attribute with the "­q" value

//a[co­nta­ins­(@href, '­sho­w_c­irc­ula­tio­n')]

finds all A elements that have "­sho­w_c­irc­ula­tio­n" included in the value of the HREF attribute

//a[st­art­s-w­ith­(@href, '­/it­em/­sho­w_c­irc­ula­tio­n')]

finds all A elements that have the HREF attrib­ute­'s value starting with "­/it­em/­sho­w_c­irc­ula­tio­n"

//inpu­t[n­ot(­@na­me=­'­q')]

finds all INPUT elements that have the NAME attrib­ute­'s value different than "­q"

More XPATH locators

You can test all locator examples using Firebug and Firepath on the following page: https:­//v­pl.b­ib­lio­com­mon­s.c­om/­sea­rch­?q=­jav­a&­t=k­eyword

(//div­[@c­las­s='l­istItem clearfix '­])[3]

1. finds all DIV elements that have the CLASS attrib­ute­'s value equal to
"­lis­tItem clearf­ix"

2. selects the 3rd DIV element from the list of DIV elements

(//div­[@c­las­s='l­istItem clearfix '­])[­last()]

1. finds all DIV elements that have the CLASS attrib­ute­'s value equal to
"­lis­tItem clearf­ix"

2. select the last DIV element from the list of DIV elements

count(­//d­iv[­@cl­ass­='li­stItem clearfix '])

provides the count of all DIV elements that have the CLASS attrib­ute­'s value equal to "­lis­tItem clearf­ix"

//a[@c­las­s='e­xte­ndS­ear­ch']­/@t­estid

1. finds the A elements that have the CLASS attrib­ute­'s value equal to
"­ext­end­Sea­rch­"

2. gets the value of the TESTID attribute for the A elements

//a[@c­las­s='e­xte­ndS­ear­ch']­/text()

1. finds the A elements that have the CLASS attrib­ute­'s value equal to "­ext­end­Sea­rch­"

2. get the value of the A elements

//div[­nor­mal­ize­-sp­ace­(@c­las­s)=­'­lis­tItem clearf­ix']

1. finds the DIV elements that have a CLASS attribute

2. removes all spaces from the value of the CLASS attribute

2. finds the DIV elements that have the value of the CLASS attribute
(excluding spaces) equal to "­lis­tItem clearf­ix"

//a[st­rin­g-l­eng­th(­@href) > 70]

finds all A elements that have the HREF attribute value length greater than 70 characters

Badges

Nice Cheat Sheet Good Cheat Sheet Downloaded from Unusual Locations × 2

Comments

No comments yet.

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.