How to use the Selenium Locators Cheat Sheet
- 1. Search or filterType a word like sibling or wait, or pick a category.
- 2. Copy the patternCopy the items you need into your notes or code.
- 3. DownloadSave the full or filtered sheet as a text file.
What is on the sheet
The cheat sheet groups locators the way you use them: the eight By strategies in Selenium WebDriver, common CSS patterns (attribute, starts-with, child, sibling, nth-child), XPath patterns (relative paths, text matching, contains, starts-with, index), XPath axes and waits.
One of the most useful lines is the label-to-input pattern //label[text()='Email']/following-sibling::input, which finds a field by its visible label even when the input has no id. CSS cannot move sideways or upwards like this, which is when XPath earns its place. Each item shows the syntax, a one-line explanation and, where useful, an example you can paste straight into a test.
Rules of thumb
- Priority: id → data-testid → name → CSS → XPath. Choose the most stable, not the cleverest.
- Never use absolute XPath copied from DevTools for real tests.
- Use explicit waits (WebDriverWait with a condition), not fixed sleeps, and do not mix implicit and explicit waits.
- Test in the console first:
$$("css")and$x("//xpath")in Chrome DevTools. - Frames and shadow DOM need a switch or a shadow root before you can find inner elements.
Syntax shown is for Selenium 4 in Java and Python. Older Selenium 3 helpers such as find_element_by_id were removed in Selenium 4. Try any pattern on your own HTML with the XPath tester, or let the locator generator suggest one.
Frequently asked questions
Is CSS faster than XPath?
In modern browsers the difference is small. Choose whichever gives the clearer, more stable locator.
Does this apply to Playwright or Cypress?
The CSS and XPath patterns do. The By and wait syntax is Selenium-specific.
Is the Selenium Locators Cheat Sheet free?
Yes, the Selenium Locators Cheat Sheet is free. The first time you download the sheet, you are asked for your name and WhatsApp number once. After that, this and every other Uncodemy tool opens straight away without the form.