Main Site ↗

selector-strategies

by omerlefaruk00GitHub

Provides concrete guidance on writing reliable CSS/XPath selectors for web automation, focusing on stable attributes like data-testid and avoiding brittle patterns. Includes priority tables, anti-pattern warnings, and handling strategies for dynamic content in React apps.

Unlock Deep Analysis

Use AI to visualize the workflow and generate a realistic output preview for this skill.

Powered by Fastest LLM

Target Audience

RPA developers, test automation engineers, and web developers writing reliable selectors for Playwright, Selenium, or Puppeteer

10/10Security

Low security risk, safe to use

9
Clarity
9
Practicality
8
Quality
8
Maintainability
6
Innovation
Testing
web-automationcss-selectorsxpathrpatest-engineering
Compatible Agents
Claude Code
Claude Code
~/.claude/skills/
Codex CLI
Codex CLI
~/.codex/skills/
Gemini CLI
Gemini CLI
~/.gemini/skills/
O
OpenCode
~/.opencode/skills/
O
OpenClaw
~/.openclaw/skills/
GitHub Copilot
GitHub Copilot
~/.copilot/skills/
Cursor
Cursor
~/.cursor/skills/
W
Windsurf
~/.codeium/windsurf/skills/
C
Cline
~/.cline/skills/
R
Roo Code
~/.roo/skills/
K
Kiro
~/.kiro/skills/
J
Junie
~/.junie/skills/
A
Augment Code
~/.augment/skills/
W
Warp
~/.warp/skills/
G
Goose
~/.config/goose/skills/
SKILL.md

Selector Strategies

Best practices for CSS and XPath selectors in RPA automation.

Core Principles

  1. Prefer stable attributes: data-testid, aria-label, name over classes/IDs
  2. Anchor to reliable parents: Combine structural + semantic selectors
  3. Avoid brittle indices: Never nth-child, :first, [1]
  4. Handle dynamic content: Use text contains, partial matches, attributes
  5. Document intent: Comment why selector was chosen

Robust Attributes (Priority Order)

AttributeStabilityNotes
data-testid★★★★★Made for testing, never changes for styling
data-*★★★★☆Custom data attributes are stable
aria-label★★★★☆Accessibility, rarely changes
name★★★★☆Form inputs, usually stable
id★★★☆☆Can be dynamic (e.g., user-12345)
class★★☆☆☆Often changes with CSS refactors

Selector Hierarchies

GOOD: Anchor → Semantic → Leaf
[data-testid="login-form"] → input[name="username"]

BAD: Deep chain of classes
div.container > div.row > div.col > input.form-control

Dynamic Element Handling

PatternCSSXPath
Text contains:has-text("Submit")//*[contains(text(),"Submit")]
Partial attr[href*="/products"]//*[contains(@href,"/products")]
Attr starts[id^="user-"]//*[starts-with(@id,"user-")]
Attr ends[class$="-active"]//*[ends-with(@class,"-active")]
Siblingh2 + p//h2/following-sibling::p[1]

Maintenance Tips

  1. Group selectors by feature: Create selector constants per page/feature
  2. Version selectors: Tag selectors with app version when known to break
  3. Fallback selectors: Provide primary + backup for critical elements
  4. Wait strategies: Combine selectors with proper wait conditions

Anti-Patterns

/* NEVER */
div:nth-child(3)           /* Brittle - breaks on DOM change */
.button:first             /* Fragile - assumes button order */
#react-root-12345         /* Dynamic ID - changes each render */
[class*="active"]         /* Too broad - matches multiple */

Examples

See examples/ folder for good/bad comparisons.


Parent: ../_index.md

Source: https://github.com/omerlefaruk/CasareRPA#.claude~skills~selector-strategies

Content curated from original sources, copyright belongs to authors

Grade A
8.4AI Score
Best Practices
Checking...
Try this Skill

User Rating

USER RATING

0UP
0DOWN
Loading files...

WORKS WITH

Claude Code
Claude
Codex CLI
Codex
Gemini CLI
Gemini
O
OpenCode
O
OpenClaw
GitHub Copilot
Copilot
Cursor
Cursor
W
Windsurf
C
Cline
R
Roo
K
Kiro
J
Junie
A
Augment
W
Warp
G
Goose