Beyond Basic Selectors
While simple selectors like.class
and #id
work well for simple tasks, complex websites often require more sophisticated approaches. Advanced CSS selectors allow you to:
- Target elements with specific attributes or patterns.
- Combine multiple conditions for greater accuracy.
- Extract data based on element relationships.
Selector Types and Examples
Websites often don’t provide convenient classes but use data attributes or dynamic IDs:Selector Type | Example | Description |
---|---|---|
Attribute Contains | [attr*="value"] | Selects elements with an attribute containing “value” |
Attribute Starts With | [attr^="value"] | Selects elements with an attribute starting with “value” |
Attribute Ends With | [attr$="value"] | Selects elements with an attribute ending with “value” |
Not Selector | :not(selector) | Excludes elements that match the selector |
Nth-child | :nth-child(n) | Selects the nth child of its parent |
Nth-of-type | :nth-of-type(n) | Selects the nth sibling of its type |
Attribute-Based Selection
Many websites use dynamic IDs or data attributes instead of simple classes. Here’s how you can target these elements:You can find more details about the CSS extractor in the CSS Extractor documentation.
Combinatorial Selectors
Combine multiple conditions to pinpoint specific elements:Selecting by Relationships
Use sibling and parent-child relationships to locate elements:Selector | Syntax | Description |
---|---|---|
Adjacent | A + B → h2 + p | Select p immediately after an h2 |
General Sibling | A ~ B → .a ~ .b | Select all .b siblings after .a |
Direct Child | A > B → ul > li | Select li that is a direct child of ul |
Dynamic Content Selection
When dealing with dynamic or JavaScript-rendered content, enablejs_render
and use flexible selectors:
Debugging Selectors
When selectors don’t work as expected:-
Inspect the Full HTML: Use ZenRows with
js_render: true
to see what the DOM actually contains -
Start Broad, Then Narrow Down:
-
Use Text-Based Debugging: Find elements by their text content:
Selector Performance Tips
Optimize your selectors for both accuracy and performance:- Avoid Universal Selectors:
*
is slow; use more specific selectors. Use class (.class
) and ID (#id
) selectors over attribute selectors for speed. - Minimize Selector Depth:
.product-grid .product .title
is faster thanbody div.container div.products div.product-grid div.product div.title
- Prefer ID and Class Selectors:
#product-123
is faster than[data-product-id="123"]
- Avoid Parent Selectors When Possible: Child (
>
) and adjacent (+
) selectors are faster than descendant selectors (space)
CSS Selector Cheat Sheet
Selector | Purpose | Example |
---|---|---|
element | Select by tag | div , span , h1 |
.class | Select by class | .product , .price |
#id | Select by ID | #main , #product-123 |
[attr] | Has attribute | [data-id] |
[attr="val"] | Exact attribute | [type="submit"] |
[attr*="val"] | Contains value | [href*="product"] |
[attr^="val"] | Starts with value | [class^="product-"] |
[attr$="val"] | Ends with value | [src$=".jpg"] |
:nth-child(n) | By position | li:nth-child(2) |
:first-child | First child | li:first-child |
:last-child | Last child | li:last-child |
:not(selector) | Negation | .item:not(.featured) |
A > B | Direct child | .product > .title |
A + B | Adjacent sibling | h2 + p |
A ~ B | General sibling | .featured ~ .product |
A, B | Multiple selectors | .price, .discount |
A B | Descendant | .product .price |