Quick wins for a faster PC:
Repair Windows errors before they cause bigger problemsFix Now →Scan for outdated or missing drivers - takes under a minuteDriver Scan →Use a unique, predictable id when the page provides one. Otherwise, Selenium recommends a well-written CSS selector; use XPath when its flexibility is needed, and verify that your locator does not match unintended elements. This cheat sheet covers Selenium’s eight traditional locator strategies, Selenium 4 relative locators, lookup behavior, and practical examples.
Selenium locator strategies at a glance
Selenium WebDriver has eight traditional locator strategies. The examples below use Python’s Selenium API, with the same strategy names applying across WebDriver language bindings.
| Strategy | What it matches | Example | Watch for |
|---|---|---|---|
id |
An element whose id attribute matches the supplied value. |
By.ID, "email" |
Prefer an ID that is unique and consistently predictable. |
name |
An element whose name attribute matches the supplied value. |
By.NAME, "username" |
Use it when the page provides an appropriate name. |
class name |
An element with the specified class. | By.CLASS_NAME, "submit-button" |
Supply one class name; a compound class string is not permitted. |
css selector |
Elements matching a CSS selector. | By.CSS_SELECTOR, "form#login input[name='email']" |
Prefer a compact, readable selector. Selenium recommends CSS when a suitable unique ID is unavailable. |
xpath |
Elements matching an XPath expression. | By.XPATH, "//input[@name='email']" |
Flexible, but Selenium notes that XPath syntax can be more complicated and harder to debug. |
link text |
An anchor whose visible text exactly matches. | By.LINK_TEXT, "Privacy policy" |
Works only on links; exact copy must remain stable. |
partial link text |
An anchor whose visible text contains the supplied text. | By.PARTIAL_LINK_TEXT, "Privacy" |
Works only on links and may match more than one link. |
tag name |
Elements with the requested tag. | By.TAG_NAME, "button" |
Often matches many elements, so it is mainly useful for collection lookups. |
These strategies and their scope are documented in Selenium’s locator strategies reference.
How to choose a locator
- Check for a stable, unique ID. Selenium says: “In general, if HTML IDs are available, unique, and consistently predictable, they are the preferred method for locating an element on a page.” See Selenium’s locator guidance.
- Use a readable CSS selector if there is no suitable ID. Keep it focused on meaningful attributes and relationships rather than relying on a long chain of incidental containers.
- Choose XPath when its flexibility solves a real need. It can express relationships and conditions that are useful for a particular page, but its added flexibility may make selectors harder to read and debug.
- For links, decide whether exact or partial text is safer. Exact text is more specific but depends on the whole visible label; partial text can survive some copy changes but risks matching another link.
- Be cautious with broad classes and tag names. If the locator can match several elements, narrow it or retrieve a collection and inspect the results.
CSS selector or XPath?
Both can locate elements by attributes and relationships. Selenium’s guidance favors a well-written CSS selector when a suitable unique ID is unavailable, while noting XPath’s greater flexibility and debugging complexity. The cited documentation does not establish that one is universally faster, so choose based on clarity and the match you need rather than assuming a speed advantage.
#1 Best Overall
Python examples: find one element or a collection
Install the Selenium Python package and have a compatible browser and driver setup before running this example. It opens a page, finds an element by ID, and collects matching buttons:
from selenium import webdriver
from selenium.webdriver.common.by import By
with webdriver.Chrome() as driver:
driver.get("https://example.com")
heading = driver.find_element(By.ID, "main-heading")
print(heading.text)
buttons = driver.find_elements(By.TAG_NAME, "button")
print(f"Found {len(buttons)} buttons")
Replace the example URL and locator values with elements that exist on your target page. The singular find_element call returns the first match in the current search context; it does not prove the match is unique. Use find_elements when you expect or need to inspect multiple matches. Selenium documents this behavior in Finding web elements.
Rank #2
Common Python locator forms
By.ID, "email"
By.NAME, "username"
By.CLASS_NAME, "submit-button" # one class name only
By.CSS_SELECTOR, "input[name='email']"
By.XPATH, "//input[@name='email']"
By.LINK_TEXT, "Privacy policy" # exact link text
By.PARTIAL_LINK_TEXT, "Privacy" # partial link text
By.TAG_NAME, "button"
Relative locators in Selenium 4
Selenium 4 also provides relative locators: above, below, to_left_of, to_right_of, and near. They locate a target by its position relative to an element you can identify more easily. Selenium uses getBoundingClientRect() to determine element size and position for this feature; see the official locator reference.
Use relative location when visual placement is genuinely the relationship your test needs. It is not a substitute for a stable semantic locator when one is available: layout changes can alter which element is above, below, or near another.
PC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchRank #3
Nested elements and shadow roots
Scope a lookup to a parent
You can locate a parent element first, then search within it. This makes the intended scope explicit, but may require two browser commands. Selenium notes that a CSS or XPath selector can sometimes describe the same search in one command and improve performance slightly. Keep the one-command selector readable rather than turning it into an unnecessarily long DOM traversal.
Search inside a shadow root
For a shadow-DOM element, first locate its host, obtain the host’s shadow root, and search within that root. Selenium’s shadow-root finder methods require Selenium 4 or later.
Rank #4
host = driver.find_element(By.CSS_SELECTOR, "my-widget")
shadow_root = host.shadow_root
button = shadow_root.find_element(By.CSS_SELECTOR, "button")
The host selector and inner selector must match the actual page. See Selenium’s finder reference for search contexts and shadow-root methods.
Common locator problems and fixes
- The wrong element is selected: a singular lookup returns the first match. Make the selector more specific, or use a collection lookup and check its members.
- A class-name lookup rejects the value: class-name strategy takes one class, not a space-separated compound string. Use a CSS selector such as
.primary.submitwhen matching multiple classes is needed. - Link text finds nothing: link-text strategies only apply to anchors. Confirm the visible text and whether exact or partial matching is appropriate; for non-links, use another strategy.
- A tag-name lookup returns too many results: tags such as
buttonare commonly repeated. Scope the search or use a more specific attribute selector. - An XPath is difficult to maintain: simplify it or use a readable CSS selector if it expresses the same target. Avoid deep paths tied to incidental page structure.
- A nested search is slower than expected: a parent lookup followed by a child lookup can require two commands. Where clarity allows, express the relationship in one CSS or XPath locator.
- A relative locator becomes unreliable after layout changes: it depends on spatial relationships. Prefer a stable ID or semantic selector if the target has one.
Or skip the browser setup
If your goal is to capture a page rather than automate interaction with its elements, ScreenshotNeo provides a website screenshot API and MCP server. A single GET request can return a PNG, JPEG, WebP, or PDF. For example, this cURL request saves a WebP screenshot; see the ScreenshotNeo documentation for parameters.
Quick Recap
Best Value
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
- Cookie/consent banners are accepted and removed, along with supported newsletter popups and chat widgets, before capture; each step can be turned off.
- Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed. Responses include
X-Page-VerdictandX-Billedheaders. - An MCP server exposes
take_screenshot,get_page_info, andcapture_pdffor Claude, Cursor, and other MCP clients. - The Free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000 shots.
Sign up for ScreenshotNeo’s free plan.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




