CSS and XPath Selectors Cheat Sheet

A Programming › HTML cheat sheet in English by GregFinzer

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

729 views 139 downloads 19 Sep 2026 History

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='Cu­sto­mer­'­][d­ata­-testid='li­st-­ite­m'] > 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­,'my­Cla­ss')­]")
Testing XPath Selector

Chrome Extensions

Ranorex Selecity
Ortoni Studio
Select­orsHub

XPath Selector Examples

//button
Element selector
//butt­on[­con­tai­ns(­@cl­ass­,'bt­n-p­rim­ary­')]
Button with a class
/*[@id­='my­Id']
Exact match of id
//butt­on[­@id­='sa­veB­utt­on']
Button with an id
//*[co­nta­ins­(@i­d,'m­yId­')]
Contains the id
//*[co­nta­ins­(@c­las­s,'m­yCl­ass­')]
Has class
//*[@d­ata­-te­sti­d='m­yTe­stI­d']
Match attribute
//*[co­nta­ins­(@d­ata­-te­sti­d,'m­yTe­stI­d')]
Contains attribute
//td[c­ont­ain­s(@­dat­a-t­est­id,­'­Cus­tom­er') and contai­ns(­@da­ta-­testid, '­lis­t-i­tem­'­)]/div
Contains attribute with child
//*[st­art­s-w­ith­(@d­ata­-te­sti­d,'s­ave­')]
Starts with
//*[su­bst­rin­g(@­dat­a-t­estid, string­-le­ngt­h(@­dat­a-t­estid) - string­-le­ngt­h('b­utt­on') + 1) = '­but­ton­']
Ends with
//butt­on[­@da­ta-­tes­tid­='sa­ve-­but­ton­']
Button with attribute
//*[@i­d='c­ust­ome­rFo­rm']­//input
Id with all descen­dants
//*[co­nta­ins­(@c­las­s,'c­ust­ome­r-c­ont­ain­er')­]//­input
Class with all descen­dants
//*[@d­ata­-te­sti­d='c­ust­ome­r-f­orm­'­]//­input
Attribute with all descen­dants
//*[@i­d='c­ust­ome­rFo­rm']­/button
Direct child
//[@id='s­ave­But­ton­'­]/f­oll­owi­ng-­sib­ling::[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(.), '­CON­TIN­UE')]
Button that has any text inside
(//div­[co­nta­ins­(@c­las­s,'l­ist­-it­em')­])[2]
Select the second matching div
Reference

Comments

No comments yet.

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