Selenium wait practice
Practice waiting: content that arrives two seconds late, a spinner that stands in for a slow request, a list that loads more as you scroll, and toasts that stack and vanish after three seconds. Open the shop catalog
Content that arrives lateTwo seconds after the page loads, or after Reload.
Built well
Built to break
Spinner
Built well
Built to break
Stacked toastsEach hides after 3 seconds. Two can show at once.
Built well
Built to break
List that loads as you scroll
Built well
- Product 1
- Product 2
- Product 3
- Product 4
- Product 5
- Product 6
- Product 7
- Product 8
- Product 9
- Product 10
Built to break
Tasks: write a locator, get a grade
How to locate Waits
The element is right, the moment is wrong
Most flaky tests are not locator problems; they are timing problems. The locator is fine, but the test looks before the content exists, or after it has been replaced. Content that loads from a server, a spinner that hides a slow request, and a list that grows as you scroll all need a test that waits for a condition.
Fixed sleeps are the usual workaround and the usual cause of slow, flaky suites: too short on a slow day, wasted time on a fast one. Wait for the thing you need instead: the element to appear, the spinner to go away, or the region to stop being busy.
Well-built dynamic content gives a test something to wait on. The good forms here mark their region aria-busy while loading, announce progress with role="progressbar", and give each loaded item a test id. The fragile forms give nothing: content is simply absent until it is not.
Toasts are the opposite problem: they appear on their own and leave on a timer. A test has to assert the message while it is still there, and when two stack it has to pick the right one. The fragile stack on this page puts the newest toast first, so “the first toast” changes with every click.
Lazy-loaded lists add scrolling. Items beyond the loaded batch are not in the DOM, so a test has to scroll or press “Load more” until the item it wants exists.
What a stable locator looks like
Wait on state, not time. Assert that the region is no longer busy or that the result is visible, and let the tool retry. Playwright and Cypress retry assertions on their own; in Selenium, use WebDriverWait with an expected condition.
// Assertions retry until the content arrives (5 seconds by default)
const recs = page.getByRole('region', { name: 'Recommendations' });
await expect(recs).toHaveAttribute('aria-busy', 'false');
await expect(recs.getByText('Aero Headphones')).toBeVisible();
await page.getByTestId('load-orders').click();
await expect(page.getByRole('progressbar', { name: 'Loading orders' })).toBeHidden();
await expect(page.getByTestId('orders-result')).toHaveText('3 orders found.');
await page.getByRole('button', { name: 'Load more' }).click();
await expect(page.getByTestId('lazy-item-20')).toBeVisible();
// Assert a toast before it hides, and pick the right one from the stack
await page.getByRole('button', { name: 'Both at once' }).click();
await expect(page.getByTestId('toast-shipped')).toHaveText('Order KG-2101 shipped.');
await expect(page.getByTestId('toast-shipped')).toBeHidden({ timeout: 5000 });In a real workflow
The shop’s catalog loads its recommendations a second after the page, and the admin’s activity log loads more entries as you scroll. Open the catalog and wait for the recommendations without a sleep.
COR Lens generates locators for shadow DOM, iframes and tables, and grades each one. Click the waits above with it and compare its pick with yours.
Add to Chrome