Prompt
How do I debug browser automation when selectors keep breaking?
Latest observation
When browser automation selectors keep breaking, the main goal is to make failures visible, reproducible, and easy to inspect. A good debugging workflow usually looks like this:
1) Stop using brittle selectors
Selectors often break because they depend on:
- generated class names
- deeply nested DOM structure
- visible text that changes
- indexes like
nth-child()or:nth-of-type()
Prefer selectors based on stable attributes:
data-testiddata-qaaria-labelrole- stable IDs
- accessible names
Examples:
- Better:
button[data-testid="save"] - Better:
input[aria-label="Search"] - Worse:
.css-1x2y3z > div:nth-child(3)
2) Inspect the element in the browser
Use DevTools to confirm:
- the element still exists
- the selector matches exactly one element
- the element is inside an iframe or shadow DOM
- the element is visible and enabled
In Chrome DevTools, try:
document.querySelectorAll('your-selector')
If this returns 0 or more than 1, that’s a clue.
3) Add screenshots and DOM dumps on failure
When a step fails, capture:
- a screenshot
- the page HTML or relevant subtree
- the current URL
- any console errors
This helps you distinguish:
- selector changed
- page not fully loaded
- popup/modal blocking interaction
- iframe/shadow DOM issue
- backend data changed
4) Wait for the right condition, not just time
A lot of selector failures are really timing issues.
Use waits for:
- element to appear
- element to become visible
- network to settle
- navigation to complete
- button to become enabled
Avoid arbitrary sleeps unless necessary.
Examples:
- Playwright:
await page.locator(selector).waitFor({ state: 'visible' }) - Selenium: explicit waits with expected conditions
5) Log what the automation sees
Before clicking or typing, log:
- the selector used
- how many matches it found
- whether it’s visible
- whether it’s detached from the DOM
- the nearest parent/container
This can quickly show whether the issue is with the selector or the page state.
6) Use locator strategies that fail loudly and clearly
A locator should ideally:
- match one element
- be stable across UI changes
- reflect user intent
For example, instead of locating by layout:
- “the third button in the header”
use semantic intent:
- “the Save button”
- “the search input”
7) Check for iframes and shadow DOM
Selectors often “break” because the element is not in the main document.
- If inside an iframe, you must switch context first.
- If inside shadow DOM, standard selectors may not work directly.
This is a very common source of confusion.
8) Watch for SPA re-renders and stale elements
In React/Vue/Angular apps, the element may be replaced after render.
Symptoms:
- element found, then click fails
- “stale element reference”
- selector works once, then not again
Fixes:
- re-query right before interaction
- avoid storing element references too long
- wait for the app to finish updating
9) Record or replay the failure
Use:
- video recording
- trace viewer
- HAR/network logs
- step-by-step screenshots
Tools like Playwright trace viewer are especially useful because they show:
- DOM snapshots
- actions taken
- network activity
- console logs
10) Build a small repro
If the automation is large, isolate the failing step into a tiny test script:
- open page
- wait
- find element
- interact with it
This makes it much easier to see exactly where the selector started failing.
Practical checklist
When a selector breaks, ask:
- Did the DOM change?
- Is the element inside an iframe/shadow root?
- Is the element visible and enabled?
- Is the page still loading or re-rendering?
- Did the app change classes/structure?
- Is the selector too specific or too generic?
- Can I replace it with a stable attribute?
Best long-term fix
If you control the app, add stable automation hooks:
data-testid="submit-order"data-test="login-button"
This is usually the most reliable way to keep automation maintainable.
If you want, I can also give you:
- a Playwright-specific debugging checklist
- a Selenium-specific debugging workflow
- or a template script that logs selector failures automatically
Brands