Prompt
How do I keep automations working when a website layout changes?
Latest observation
To keep automations working when a website layout changes, build them to be resilient to UI changes rather than tied to exact page structure.
Practical strategies
1) Prefer stable selectors
Use attributes that are less likely to change:
idnamedata-*attributesaria-label- unique text labels
Avoid fragile selectors like:
- long absolute XPath
- positional CSS selectors like
div > div:nth-child(3) > ... - classes generated by frameworks that change often
2) Target behavior, not layout
Instead of selecting by where something appears on the page, select by:
- button text
- form labels
- link destinations
- element roles
Example:
- Better:
button[aria-label="Save"] - Worse:
div.header > div.actions > button:nth-child(2)
3) Add fallback selectors
If one locator fails, try a backup:
- primary selector
- alternate text-based selector
- nearby label-based selector
This helps when small markup changes happen.
4) Use page semantics
For browser automation, use:
- accessible roles
- labels
- form associations
Examples:
getByRole("button", { name: "Submit" })getByLabel("Email")
These are usually more robust than raw DOM paths.
5) Wait for the right state
Many failures happen because the page is still loading or elements are dynamic. Use:
- explicit waits for visibility/clickability
- network idle or page-ready checks
- retries for transient conditions
Avoid fixed sleep delays unless necessary.
6) Handle optional and moving content
If sections may appear/disappear:
- check existence before interacting
- don’t assume a fixed order
- search within a container instead of the whole page
7) Keep automation logic modular
Separate:
- navigation
- element-finding
- form filling
- validation
That way, if the layout changes, you only update the locator logic, not the entire automation.
8) Monitor and detect breakage early
Add:
- screenshots on failure
- DOM or HTML snapshots
- logging of failed selectors
- alerts when automation success rate drops
This makes layout changes visible quickly.
9) Use APIs when possible
If your automation is doing a task that the site supports through an API, prefer the API over UI automation. APIs are much more stable than page layouts.
10) Build maintenance into the process
Expect occasional updates:
- keep selectors in one config/file
- version your automations
- periodically test against staging or a test account
Good rule of thumb
If a selector depends on visual position, it’s probably fragile.
If it depends on meaning or stable attributes, it’s much safer.
If you want, I can also show:
- a Playwright example
- a Selenium example
- or a selector checklist for making automations more durable.
Brands