# Selector Strategies Guide ## Selector Types Overview | Selector Type | Stability | Speed | Best For | |--------------|-----------|-------|----------| | CSS ID | ⭐⭐⭐⭐⭐ | Fast | Unique IDs | | Smart Mode (text) | ⭐⭐⭐⭐ | Medium | Text-based UI | | XPath (text) | ⭐⭐⭐⭐ | Medium | Text content | | CSS Class | ⭐⭐ | Fast | Stable classes | | XPath (structure) | ⭐⭐ | Medium | DOM structure | ## Decision Tree ``` Does element have unique ID? ├─ YES → Use CSS: #element-id └─ NO ↓ Is element identified by text? ├─ YES → Use Smart Mode: --text "Submit" └─ NO ↓ Does element have stable class? ├─ YES → Use CSS: .stable-class └─ NO ↓ Can use ARIA attributes? ├─ YES → Use XPath: //*[@role='button'] └─ NO ↓ Use structural XPath ``` ## CSS Selectors ### By ID (Most Stable) ```bash node .browser-pilot/bp click -s "#login-button" node .browser-pilot/bp click -s "#user-menu" ``` ### By Class ```bash node .browser-pilot/bp click -s ".submit-btn" node .browser-pilot/bp click -s ".modal .close-button" ``` ### By Attribute ```bash node .browser-pilot/bp fill -s "input[name='email']" -v "test@example.com" node .browser-pilot/bp click -s "button[data-action='submit']" ``` ### Complex Selectors ```bash # Direct child node .browser-pilot/bp click -s "div.modal > button.primary" # Descendant node .browser-pilot/bp click -s "form .submit-button" # Nth-child node .browser-pilot/bp click -s "ul > li:nth-child(3)" # Multiple classes node .browser-pilot/bp click -s "button.btn.btn-primary.btn-lg" ``` ## XPath Selectors ### Text-Based (Most Reliable) **With Tag Name (Recommended):** ```bash # Specific tag with text node .browser-pilot/bp click -s "//button[contains(text(), 'Submit')]" node .browser-pilot/bp click -s "//a[contains(text(), 'Learn More')]" # Exact text match node .browser-pilot/bp click -s "//button[text()='Sign In']" ``` **With Wildcard (Avoid):** ```bash # Searches all elements (slow, imprecise) node .browser-pilot/bp click -s "//*[contains(text(), 'Submit')]" ``` ### Indexed XPath (Duplicates) ```bash # First match node .browser-pilot/bp click -s "(//button[contains(text(), 'Delete')])[1]" # Third match node .browser-pilot/bp click -s "(//button[contains(text(), 'Delete')])[3]" # Last match (if 5 exist) node .browser-pilot/bp click -s "(//button[contains(text(), 'Delete')])[5]" ``` ### Attribute-Based ```bash # By type node .browser-pilot/bp fill -s "//*[@type='email']" -v "test@example.com" # By role node .browser-pilot/bp click -s "//*[@role='button']" # By data attribute node .browser-pilot/bp click -s "//*[@data-testid='submit']" # Partial match node .browser-pilot/bp click -s "//*[contains(@href, 'checkout')]" ``` ### Structural XPath ```bash # Parent-child node .browser-pilot/bp click -s "//div[@class='modal']//button[@type='submit']" # Following sibling node .browser-pilot/bp click -s "//h1[contains(text(), 'Welcome')]/following-sibling::button" # Preceding sibling node .browser-pilot/bp click -s "//button[contains(text(), 'Next')]/preceding-sibling::button" # Parent node .browser-pilot/bp click -s "//button[@type='submit']/parent::form" ``` ## Smart Mode Selectors ### Basic Text Search ```bash node .browser-pilot/bp click --text "Submit" node .browser-pilot/bp click --text "Add to Cart" node .browser-pilot/bp fill --text "Username" -v "test" ``` ### With Type Filter ```bash # Only buttons node .browser-pilot/bp click --text "Submit" --type button # Only links node .browser-pilot/bp click --text "Learn More" --type a # Only text inputs node .browser-pilot/bp fill --text "Email" -v "test@example.com" --type input-text ``` ### Type Aliases (Auto-Expanded) ```bash # Generic type (matches all subtypes) node .browser-pilot/bp click --text "Search" --type input # Expands to: input, input-text, input-search, input-password, etc. # Specific type (exact match) node .browser-pilot/bp fill --text "Email" --type input-search -v "query" # Matches only: input-search # Button aliases node .browser-pilot/bp click --text "Submit" --type button # Expands to: button, button-submit, button-reset, etc. ``` ### Tag-Based Search ```bash # Filter by HTML tag (broader matching) node .browser-pilot/bp click --text "Submit" --tag button # Matches all