Free tools Windows power users keep installed
One-click scans. No signup required.
Short answer: use a unique, predictable ID when one is available. Otherwise, Selenium’s guidance favors a well-written CSS selector for ordinary element matching. Choose XPath when its ability to express a relationship or condition makes the locator clearer. Keep either locator readable and scoped to the smallest practical part of the page.
How Selenium’s guidance compares them
Selenium supports both css selector and xpath as WebDriver locator strategies. Its guidance recommends a unique ID when available and, if unique IDs are unavailable, a well-written CSS selector. It also says XPath works as well as CSS selectors but can be complicated and difficult to debug. Selenium cautions that XPath selectors may be slow because browser vendors typically do not performance-test them.
That is qualitative guidance, not a controlled, current comparison of browser speeds. It does not establish that CSS is always faster, more reliable, or more resilient to page changes. If speed matters, measure the locators in the browsers and pages your tests actually use.
Choose a locator by the job it needs to do
Start with a stable ID
If the target has an ID that is unique and expected to remain stable, use it. For example, Selenium’s locator reference illustrates a CSS locator such as #fname. An ID is useful only if it identifies the intended element consistently; a generated or frequently changing ID may not be a good test hook.
#1 Best Overall
Use CSS for ordinary matching
When there is no suitable unique ID, start with CSS for common cases such as matching an element’s class, attributes, or position beneath a known container. CSS selectors can express these matches directly, and Selenium’s guidance recommends a well-written CSS selector as the default in this situation.
Use XPath when its expression is clearer
XPath is a good fit when a needed condition or relationship is easier to state in XPath than in CSS. Selenium’s reference illustrates an attribute match with //input[@value='f']. Prefer an expression that describes the intended target over a long absolute path tied to incidental nesting in the current page.
Rank #2
Examples in Selenium
These Python examples use Selenium’s standard By locator strategies. The selector strings are syntax illustrations; replace them with locators that match your application’s maintained markup.
from selenium.webdriver.common.by import By
# A stable, unique ID expressed as CSS
submit = driver.find_element(By.CSS_SELECTOR, "#submit")
# Match an input by its value with XPath
field = driver.find_element(By.XPATH, "//input[@value='f']")
# Match an element by an attribute with CSS
email = driver.find_element(By.CSS_SELECTOR, "input[name='email']")
# Find a button inside a known container with XPath
save = driver.find_element(By.XPATH, "//form[@id='profile']//button[@type='submit']")
Use find_element when the test expects one matching element; Selenium returns the first match. Use the plural find method when the test needs the collection of matches. A locator that unexpectedly matches several elements is often a sign to narrow its scope or make its identifying condition more specific.
Recommended Free Tools
Rank #3
Keep locators readable and scoped
- Prefer a stable identity over a selector built from many incidental classes or nesting levels.
- Keep the locator compact enough that a maintainer can tell what distinguishes the target.
- Scope a lookup to a relevant container when that makes the intent clearer and reduces broad DOM traversal.
- When using a nested lookup, consider expressing it as one CSS or XPath locator rather than issuing multiple browser commands, if the combined locator remains understandable.
- Choose CSS or XPath based on clarity for the actual condition—not habit or an unsupported claim of universal speed.
Performance: measure before switching
Selenium’s documentation cautions that XPath may be slow, but the reviewed guidance does not provide a controlled, current cross-browser benchmark or a speed multiplier. Runtime can depend on the page, browser, and locator. Do not rewrite clear XPath locators solely on the assumption that CSS is always faster. If locator performance is material, compare the alternatives in the real test environment and workload, while keeping the target and test conditions equivalent.
Common locator problems and fixes
The locator matches the wrong element
A singular find returns the first match, which may not be the element the test intended. Inspect how many elements match, then add a meaningful condition or scope the lookup to a specific container.
Rank #4
The locator breaks after a markup change
A selector based on deep nesting or incidental classes depends on structure that may change without changing the page’s user-facing behavior. Prefer a unique stable ID when available, or use a compact CSS or XPath locator tied to the target’s meaningful identity or relationship.
The XPath is hard to debug
Break down what the expression is meant to identify and remove path segments that do not distinguish the target. If a CSS selector communicates the same condition more plainly, use CSS instead.
Best Value
The test is slow and XPath is suspected
The Selenium guidance supports a performance caution, not a universal diagnosis. Measure the relevant operation on the actual page and browser before changing strategies; also check whether the locator is traversing an unnecessarily broad part of the document.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Or skip the browser setup
If your goal is to capture a website screenshot rather than locate elements in a Selenium test, ScreenshotNeo is a separate option: it provides a screenshot API and MCP server, not a replacement for Selenium selectors. One GET request can return an image or PDF. For example, save a WebP screenshot of Stripe with cURL:
Quick Recap
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
See the ScreenshotNeo API documentation for request options. It removes cookie banners, newsletter popups, and chat widgets before capture; bot checks, blank pages, and failed loads are not billed. Its MCP server lets AI agents take screenshots, and the Free plan includes 1,000 screenshots per month without a card. Paid plans start at $5 for 3,000 shots. Sign up for 1,000 free screenshots a month with no card.
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.
Quick wins for a faster PC:
Clear out junk files and repair common Windows errorsFree Scan →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Repair Windows errors before they cause bigger problemsFix Now →




