Most Exhaustive WebDriver Locators Cheat Sheet

Most Exhaustive WebDriver Locators Cheat Sheet

As you know, I am keen on every kind of automation especially related to web technologies. So, I enjoy using Selenium WebDriver. You can find lots of materials in my WebDriver Series. A big part of the job of writing maintainable and stable web automation is related to finding the proper element’s locators. So, I created the first and most exhaustive Selenium WebDriver cheat sheet dedicated to the locators. I hope that you will find it quite useful. Enjoy!

Download Most Exhaustive WebDriver Locators Cheat Sheet PDF

Initially, I created the cheat sheet while we developed the first versions of the BELLATRIX automated testing framework. Мost of the stuff in it are still relevant.

Default FindElement WebDriver Methods

_driver.FindElement(By.Id("userName"));
_driver.FindElement(By.ClassName("panel other"));
_driver.FindElement(By.CssSelector("#userName"));
_driver.FindElement(By.LinkText("Automate The Planet"));
_driver.FindElement(By.Name("webDriverCheatSheet"));
_driver.FindElement(By.PartialLinkText("Automate"));
_driver.FindElement(By.TagName("a"));
_driver.FindElement(By.XPath("//*[@id='panel']/div/h1"));

Default FindsBy WebDriver Attributes

[FindsBy(How = How.Id, Using = "userName")]
[FindsBy(How = How.ClassName, Using = "panel other")]
[FindsBy(How = How.CssSelector, Using = "#userName")]
[FindsBy(How = How.LinkText, Using = "Automate The Planet")]
[FindsBy(How = How.Name, Using = "webDriverCheatSheet")]
[FindsBy(How = How.PartialLinkText, Using = "Automate")]
[FindsBy(How = How.TagName, Using = "a")]
[FindsBy(How = How.XPath, Using = "//*[@id='panel']/div/h1")]
private IWebElement _myElement;

Complete XPath Locators’ List

XPath LocatorExplanation
//imgimage element
//img[@id='myId']image element with @id= ‘myId’
//img[@id!='myId']image elements with @id not equal to ‘myId’
//img[@name]image elements that have name attribute
//*[contains(@id, 'Id')]element with @id containing
//*[starts-with(@id, 'Id')]element with @id starting with
//*[ends-with(@id, 'Id')]element with @id ending with
//*[matches(@id, 'r')]element with @id matching regex ‘r’
//*[@name='myName']image element with @name= ‘myName’
//*[@id='X' or @name='X']element with @id X or a name X
//*[@name="N"][@value="v"]element with @name N & specified @value ‘v’
//*[@name="N" and @value="v"]element with @name N & specified @value ‘v’
//*[@name="N" and not(@value="v")]element with @name N & not specified @value ‘v’
//input[@type="submit"]input of type submit
//section[//h1[@id='hi']]returns <section> if it has an <h1> descendant with @id= ‘hi’
//table[count(tr) > 1]return table with more than 1 row
//*[.="t"]element containing text ‘t’ exactly
//a[contains(text(), "Log Out")]anchor with inner text containing ‘Log Out’
//a[not(contains(text(), "Log Out"))]anchor with inner text not containing ‘Log Out’
//a[@href="url"]anchor with target link ‘url’
//img/*[1]first child of element img
//ul/child::lifirst child ‘li’ of ‘ul’
//img[1]first img child
//img/*[last()]last child of element img
//img[last()]last img child
//img[last()-1]second last img child
//input/following-sibling::a’a’ following some sibling ‘input’
//a/following-sibling::*sibling element immediately following ‘a’
//input/preceding-sibling::a’a’ preceding some sibling ‘input’
//input/preceding-sibling::*[1]sibling element immediately preceding ‘input’
//img[@id='MyId']::parent/*the parent of image with id
//*[@id="TestTable"]//tr[3]//td[2]cell by row and column
//td[preceding-sibling::td="t"]cell immediately following cell containing ‘t’ exactly
//td[preceding-sibling::td[contains(.,"t")]]cell immediately following cell containing ‘t’
//input[@checked]checkbox (or radio button) that is checked
//a[@disabled]all ‘a’ elements that are disabled
//a[not(@disabled)]all ‘a’ elements that are not disabled
//a[@price > 2.50]’a’ with price > 2.5
//ul[*]’ul’ that has children

Complete CSS Selectors’ List

CSS LocatorExplanation
ul#myId’ul’ element with @id= ‘myId’
#myUniqueIdany element with @id= ‘myId’
ul.myForm’ul’ element with @class = ‘myForm’
.myForm.frontany element with @classes = ‘myform’ and ‘front’
ul#myUniqueId > lidirect child element
ul#myUniqueId lisub child element
ul[name = "automateName"][style = "style_name"]’ul’ element with attributes @name =‘automateName’ and @style= ‘style name’
ul[id = "myId"]’ul’ element with @id=‘myId’
ul[@id]elements with @id attribute
*[name='N'][value='v']elements with name N and specified value ‘v’
ul[id ^= "my"]all elements with an attribute beginning with ‘my’
ul[id$= "Id"]all elements with an attribute ending with ‘Id’
ul[id *= "unique"]all elements with an attribute containing the substring ‘unique’
ul[id ~= "unique"]all elements with an attribute containing the word ‘unique’
ul#myUniqueId li:first-childfirst child element
ul#myUniqueId li:nth-of-type(1)first child element
ul#myUniqueId li:last-childlast child element
ul#myUniqueId li:nth-of-type(3)last child element
div > pall <p> elements that are a direct descendant of a <div> element
div + pall <p> elements that are the next sibling of a <div> element (i.e. placed directly after)
div ~ pall <p> elements that follow, and are siblings of <div> elements
a:linkall unvisited links
a:visitedall visited links
a:hoverall links on mouse hover
input:activeevery active <input> element
input:disabledevery disabled <input> element
input:enabledevery enabled <input> element
input:focusthe <input> element which has focus
input:read-only<input> elements with the ‘readonly’ attribute specified
input:required<input> elements with the ‘required’ attribute specified
input:checkedcheckbox (or radio button) that is checked
form myForm.front + ulnext sibling
a:contains('Log Out')anchor with inner text containing ‘Log Out’
a[href='url']anchor with target link ‘url’
#TestTable tr:nth-child(3) td:nth-child(2)cell by row and column (e.g. 3rd row, 2nd column)
td:contains('t') ~ tdcell immediately following cell containing ‘t’
p:lang(language)all <p> elements with a @lang attribute equal to ‘language’

Download Most Exhaustive WebDriver Locators Cheat Sheet PDF

Related Articles

Web Automation

Selenium C# MSTest Test Automating Angular, React, VueJS and 20 More

In the new article from the Web Automation Series with C#, we will talk about creating a data-driven MSTest test automating all major web technologies such as R

Selenium C# MSTest Test Automating Angular, React, VueJS and 20 More

Mobile Automation, Resources

Most Complete Appium C# Cheat Sheet

The next article from the mobile test automation series will be dedicated to Appium. All you need to to know – from the most basic operations to the most advanc

Most Complete Appium C# Cheat Sheet

Web Automation

Advanced Web UI Components Automation with WebDriver C#

Most of the websites out there use commercial Web UI Components for their front end.  Most of these components are JavaScript-based, and their test automation i

Advanced Web UI Components Automation with WebDriver C#

Resources, Web Automation Java

Most Complete Selenium WebDriver Java Cheat Sheet

As you know, I am a big fan of Selenium WebDriver. You can find tonnes of useful Java code in my Web Automation Java Series. I lead automated testing courses an

Most Complete Selenium WebDriver Java Cheat Sheet

Web Automation

Design Grid Control Automated Tests Part 3

In my previous articles Design Grid Control Automated Tests Part 1 and Design Grid Control Automated Tests Part 2 I started a mini-series about writing decent g

Design Grid Control Automated Tests Part 3

Web Automation

Design Grid Control Automated Tests Part 2

In my previous article Design Grid Control Automated Tests Part 1 I started this mini-series about writing decent grid control's automated tests. In this second

Design Grid Control Automated Tests Part 2
Anton Angelov

About the author

Anton Angelov is Managing Director, Co-Founder, and Chief Test Automation Architect at Automate The Planet — a boutique consulting firm specializing in AI-augmented test automation strategy, implementation, and enablement. He is the creator of BELLATRIX, a cross-platform framework for web, mobile, desktop, and API testing, and the author of 8 bestselling books on test automation. A speaker at 60+ international conferences and researcher in AI-driven testing and LLM-based automation, he has been recognized as QA of the Decade and Webit Changemaker 2025.