1.7 KiB
1.7 KiB
Selector Strategy
The source catalog already mixes stable and brittle selectors. Use this order when validating or refreshing selectors.
Preferred Order
- Stable
hrefselectors for direct links aria-labelandroleselectors for tabs, menus, and buttonsdata-testidselectors when available- Stable semantic class names tied to product structure
- Generic class selectors only as a last resort
- CSS hash classes only when no better hook exists
Good Patterns In The Current Catalog
a[href='/']a[href='/hot']a[href='/creator']button[aria-label='通知'][role='tab'][aria-label*='回复'][data-testid='sort-button']
These are relatively resilient because they describe user-facing semantics instead of transient layout implementation.
Known Brittle Or Weak Patterns
div.css-1q62b6s > div.css-byu4bybutton:has(img).MoreButton.Popover.Tooltip.floating-menu.Modal.Dialog
Risks:
- hash classes can change on any frontend build
- generic popup selectors can match the wrong layer
- image-based button matching is vulnerable to layout and icon changes
Revalidation Rule
Before relying on a weak selector:
- Check whether an
href,aria-label,role, ordata-testidselector now exists. - Confirm the selector matches exactly one intended element.
- Confirm the element is visible and actionable in the current page state.
- If the selector is still generic, pair it with a stronger page-context check before acting.
Failure Handling
If a weak selector stops working:
- do not silently substitute another generic selector
- report which selector failed
- describe the page context where it failed
- request a selector refresh or DOM inspection before retrying