Dynamic id practice
Practice locating elements whose ids and classes are build output. The form on the right gets new ids like billing-email-48213 and new css- classes every release; the one on the left has labels and test ids. Write a locator, then switch to v2 and check it again. Open the shop catalog
Profile formSame form twice. Switch to v2 and watch the ids on the right change.
Built well
Built to break
Tasks: write a locator, get a grade
How to locate Generated ids
An id that changes every build is not an id
Many frameworks generate ids and classes at build time: React’s useId, Angular’s mat-input-3, Ember’s ember412, CSS-in-JS classes such as css-1k9zq2 and sc-bdVaJa. They look unique and DevTools offers them first, so a recorded locator often uses them. The next build renumbers them and the test fails while the page looks the same.
A number at the end of an id is the usual tell. Two releases of this page give the email field two different ids, and the generated classes change with them. A starts-with(@id, 'billing-email') XPath survives the renumbering but breaks as soon as a second field shares the prefix.
Generated values are not always numbers. Hash-like tokens, ids that include a component path, and classes with a random suffix all change on rebuild. When in doubt, compare the same element on two builds.
What a stable locator looks like
Use what a person sees and what the team controls: the field’s label, its role and name, or a test id. The good form keeps data-testid, labels and names identical on every release, so the same locator passes on v1 and v2.
// Labels and test ids are the same on every release
await page.getByLabel('Full name').fill('Ada Park');
await page.getByTestId('gen-email').fill('[email protected]');
await page.getByRole('button', { name: 'Save' }).click();
await expect(page.getByTestId('gen-result')).toHaveText('Profile saved.');In a real workflow
In the shop catalog, the sort control and price slider carry generated classes while the product cards and result count keep test ids. Open the catalog, sort by price, read the result count, then switch to v2 and run the same steps.
COR Lens generates locators for shadow DOM, iframes and tables, and grades each one. Click the generated ids above with it and compare its pick with yours.
Add to Chrome