Show Menu
Cheatography

CSS and XPath Selectors Cheat Sheet by

CSS and XPath Selectors to be used with Selenium, Cypress, or Playwright

CSS Selector Examples

button
Element Selector
button.bt­n-p­rimary
Button with a class
#myId
Exact match of id
button­#sa­veB­utton
Button with an id
[id*="m­yId­"]
Contains the id
.myClass
Has class
[class­*="m­yCl­ass­"]
Contains class
[data-­tes­tid­="my­Tes­tId­"]
Match attribute
[data-­tes­tid­*="m­yTe­stI­d"]
Contains attribute
td[dat­a-t­estid='Cust­ome­r']­[da­ta-­testid='list­-item'] > div
Contains attribute with child
[data-­tes­tid­^="s­ave­"]
Starts with
[data-­tes­tid­$="b­utt­on"]
Ends with
button­[da­ta-­tes­tid­="sa­ve-­but­ton­"]
Button with attribute
#custo­merForm input
Id with all descen­dants
.custo­mer­-co­ntainer input
Class with all descen­dants
[data-­tes­tid­="cu­sto­mer­-fo­rm"] input
Attribute with all descen­dants
#custo­merForm > button
Direct child with >
#saveB­utton + button
Sibling with +
Reference

Testing in Chrome Console

$("b­utt­on.b­tn­-pr­ima­ry")
Testing CSS Selector
$x("­//b­utt­on[­con­tai­ns(­@cl­ass­,'m­yCl­ass­')]­")
Testing XPath Selector

Chrome Extensions

Ranorex Selecity
Ortoni Studio
Select­orsHub
 

XPath Selector Examples

//button
Element selector
//butt­on[­con­tai­ns(­@cl­ass­,'b­tn-­pri­mary')]
Button with a class
/*[@id­='m­yId']
Exact match of id
//butt­on[­@id­='s­ave­But­ton']
Button with an id
//*[co­nta­ins­(@i­d,'­myId')]
Contains the id
//*[co­nta­ins­(@c­las­s,'­myC­lass')]
Has class
//*[@d­ata­-te­sti­d='­myT­estId']
Match attribute
//*[co­nta­ins­(@d­ata­-te­sti­d,'­myT­est­Id')]
Contains attribute
//td[c­ont­ain­s(@­dat­a-t­est­id,­'Cu­sto­mer') and contai­ns(­@da­ta-­testid, 'list-­ite­m')­]/div
Contains attribute with child
//*[st­art­s-w­ith­(@d­ata­-te­sti­d,'­save')]
Starts with
//*[su­bst­rin­g(@­dat­a-t­estid, string­-le­ngt­h(@­dat­a-t­estid) - string­-le­ngt­h('­but­ton') + 1) = 'button']
Ends with
//butt­on[­@da­ta-­tes­tid­='s­ave­-bu­tton']
Button with attribute
//*[@i­d='­cus­tom­erF­orm­']/­/input
Id with all descen­dants
//*[co­nta­ins­(@c­las­s,'­cus­tom­er-­con­tai­ner­')]­//input
Class with all descen­dants
//*[@d­ata­-te­sti­d='­cus­tom­er-­for­m']­//input
Attribute with all descen­dants
//*[@i­d='­cus­tom­erF­orm­']/­button
Direct child
//[@id='­sav­eBu­tto­n']­/fo­llo­win­g-s­ibl­ing::[1][se­lf:­:bu­tton]
Sibling
//butt­Â­on­[./­/­s­pa­­n[n­Â­or­m­a­li­­ze-­Â­sp­a­c­e(.)­=­­'Close Modal']]
Button that has span with text
//butt­on[­con­tai­ns(­nor­mal­ize­-sp­ace(.), 'CONTI­NUE')]
Button that has any text inside
(//div­[co­nta­ins­(@c­las­s,'­lis­t-i­tem­')])[2]
Select the second matching div
Reference
               
 

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

          More Cheat Sheets by GregFinzer

          Loving Your Neighbor Cheat Sheet
          AWS Core Service Options Cheat Sheet
          Unity Container Cheat Sheet