How to find the right CSS selector for scraping
Right-click the element in Chrome, choose Inspect, and look for a stable class or attribute shared by all the items you want, such as class="product-title". Use that as .product-title. Avoid auto-generated class names and long copied selector paths, which match only one element or break when the site updates.
Do this in one click with LightCube Data Extractor, a free Chrome extension. No account needed.
Add to Chrome, free ↗Step by step
- Right-click one example of the data and choose Inspect.
- In the Elements panel, look at the highlighted tag and its class or data attributes.
- Check another example to confirm it shares the same class.
- Test in the Console:
document.querySelectorAll(".product-title").lengthshould equal the number of items you expect. - Paste the selector into the extension Selector mode, or use Pick on page.
Stable vs fragile selectors
| Fragile | Better |
|---|---|
#root > div:nth-child(3) > div > span | .product-title |
.css-1x2y3z | [data-testid="price"] |
| Copy selector from DevTools | A class that describes the content |
Narrowing the scope
If .title also matches the sidebar, prefix the container: main .title or .results .title.
When a site has no good classes
Try attribute selectors such as [itemprop="name"], which many shops use for structured data.
FAQ
- What does "Copy selector" in DevTools give me?
- A path to that one element; it usually needs simplifying to match all items.
- Why does my selector match zero items?
- The content may load later, sit in an iframe, or the class changed. Scroll, wait and retry.
- Is Pick on page free?
- It is part of Selector mode, which has 3 free uses per day.
Related guides
Updated 2026-11-11.