Optexity provides multiple ways to locate elements. Understanding when to use each method is key to building robust automations.
Locator Methods
| Method | Field | When to Use |
|---|
| Playwright Command | command | Preferred—robust, fast, no LLM tokens |
| XPath | xpath | Complex DOM traversal |
| AI Fallback | prompt_instructions | Always provide—used when locators fail |
Playwright Commands
Role-Based (Recommended)
Most resilient—doesn’t depend on CSS classes or IDs:
Label-Based
Great for form fields:
Text-Based
Find by visible text:
Test ID
For elements with data-testid:
CSS Selector
Direct CSS access:
Chaining
Narrow down to specific elements:
Iframes
Access elements inside iframes:
XPath Locators
Use when Playwright locators can’t express the query:
Common Patterns
XPath locators are more fragile than Playwright commands. Prefer command when possible.
Prompt Instructions
Natural language description used when locators fail:
Writing Good Instructions
Good:
Poor:
Include visual details, position, and context in prompt instructions.
Locator Selection Strategy
Dynamic Elements
Use variables in locators:
Always provide prompt_instructions as backup:
assert_locator_presence
Verify element exists before acting (for optional steps):
When true:
- Checks if locator finds element
- Acts only if the element is present
- Fails the automation with a clear error reason if the element is not found