Show Menu
Cheatography

cheat sheet with xpath expressions

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­spot.ca.

//input

finds all INPUT elements that are included in the web page

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

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

//butt­on[­@da­ta-­id=­'so­rtS­ele­cto­r']­/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='­lis­tItem 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=­'gl­oba­lQuery' 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, 'show_­cir­cul­ati­on')]

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, '/item­/sh­ow_­cir­cul­ati­on')]

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='­lis­tItem 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='­lis­tItem clearfix '])[la­st()]

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­='l­istItem 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='­ext­end­Sea­rch­']/­@testid

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='­ext­end­Sea­rch­']/­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)=­'li­stItem clearfix']

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
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Selenium WebDriver Cheat Sheet Cheat Sheet
          Cypressio Cheat Sheet
          ISTQB Test Automation Engineering Cheat Sheet