Prompt
I need browser automation that won’t break when the site updates
Latest observation
If you mean robust browser automation, the biggest rule is: don’t automate by brittle selectors or UI details. Build it like a contract with the site, not like a screen-scraper.
Best practices for durable browser automation
1) Prefer official APIs over UI automation
If the site has an API, use that instead of clicking around in the browser.
UI automation is always more fragile than API-based automation.
2) Use stable selectors
Avoid:
- CSS classes generated by frameworks
- deeply nested selectors
- XPath tied to layout structure
- text that changes frequently
Prefer:
data-testiddata-qaaria-label- stable IDs
- semantic roles (
button,textbox, etc.)
Example:
<button data-testid="save-profile">Save</button>
3) Ask the site owner to add automation hooks
If you control the site, add attributes specifically for automation:
data-testid="login-button"data-automation="checkout-submit"
This is one of the best ways to make tests resilient.
4) Select by behavior, not layout
Use:
- role + accessible name
- form labels
- button text only if it’s stable
Example in Playwright:
await page.getByRole('button', { name: 'Save' }).click();
await page.getByLabel('Email').fill('a@example.com');
5) Wait for states, not time
Never rely on fixed sleeps like wait(5000).
Wait for:
- element visible
- navigation complete
- network idle
- specific text present
Example:
await page.waitForURL(/dashboard/);
await page.getByText('Welcome back').waitFor();
6) Make locators self-healing where possible
Use fallback strategies:
- primary selector
- secondary selector
- text/role fallback
- retry with page reload if a known transient issue happens
7) Separate intent from implementation
Structure automation as:
- “log in”
- “create order”
- “submit form”
Not:
- click x
- click y
- type z
This makes updates easier to absorb.
8) Add monitoring and alerting
When automation fails:
- capture screenshot
- save HTML snapshot
- log selector failures
- track which step broke
That way you can fix it quickly when the site changes.
9) Use visual testing for UI regressions
If the app changes often, combine automation with:
- screenshot comparisons
- DOM diff checks
- accessibility tree checks
10) Expect change
No browser automation is truly “won’t break” if the UI changes enough. The goal is to reduce breakage and detect it early.
Recommended tool choices
- Playwright: excellent for resilient browser automation
- Selenium: widely used, but more fragile unless carefully designed
- Cypress: good for app testing, less ideal for some cross-tab flows
If your goal is resilience, I’d usually recommend Playwright with:
- role-based locators
- test IDs
- explicit waits
- screenshots on failure
Example resilient Playwright locator style
await page.getByRole('button', { name: /submit order/i }).click();
await page.getByLabel('Phone number').fill('555-1234');
await expect(page.getByText('Order confirmed')).toBeVisible();
If you want automation that survives site updates best
The strongest approach is:
- Use the API if available
- If UI is required, use semantic locators
- Work with the site owner to add automation attributes
- Add good error handling and monitoring
If you want, I can help you design a site-update-resistant Playwright framework or review a specific page and suggest durable selectors.