October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk5 min

How to Wait for Elements to Disappear in a Single-Page App

Learn the difference between an element becoming hidden and being removed, with state-based wait examples for Playwright, Selenium Python, and Cypress.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Wait for the specific state your test needs: use hidden when the element must no longer be visible, and detached or not exist when it must be removed from the DOM. In a single-page app, use a framework wait or retrying assertion rather than a fixed sleep; the page can keep changing after its initial document load.

Choose what “disappear” means

An element can stop appearing on screen while remaining mounted in the DOM—for example, because it is hidden. Removal is a different result: the element is no longer present. Choose the condition that matches the behavior under test, such as a spinner becoming invisible versus a modal being unmounted.

As an Amazon Associate I earn from qualifying purchases.

  • Wait for hidden: use when the user should no longer see the element, whether it is hidden or removed.
  • Wait for removal: use when the test specifically requires the element to leave the DOM.

Playwright

Use a locator’s waitFor method. The hidden state succeeds if the element is hidden or detached; use detached when DOM removal is required. Playwright supports attached, detached, visible, and hidden states. A state that already holds satisfies the wait immediately; if it does not arrive before the configured timeout, the call throws. For hidden and detached, the method returns null. See the Playwright Locator API.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
await page.locator('[data-testid="loading"]').waitFor({ state: 'hidden' });

For strict removal, change the state:

await page.locator('[data-testid="loading"]').waitFor({ state: 'detached' });

Prefer locators and web-first assertions in current Playwright code. The older selector-based Page APIs are discouraged in favor of locator-based waits and assertions. Do not use page.isHidden() as a wait: it checks immediately, and its timeout option is ignored. The API documentation covers Page.isHidden.

Selenium with Python

Use WebDriverWait with an inverse visibility condition when the element must no longer be visible. This example follows Selenium’s Python API; adapt the locator tuple to your selector. For a CSS selector, use (By.CSS_SELECTOR, '.loading').

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

WebDriverWait(driver, 10).until_not(
    EC.visibility_of_element_located((By.ID, "exampleId"))
)

The wait polls the supplied condition until it reaches the requested result or times out. For a direct predicate, Selenium’s Python API also demonstrates until_not(lambda x: x.find_element(...).is_displayed()). This waits for no longer visible, not necessarily for DOM removal. Use a condition that expresses the required state if your test must assert absence. See the Selenium Python WebDriverWait API.

Single-page apps can update after document readiness, so synchronization with the expected state is more reliable than guessing a delay. Selenium warns not to mix implicit and explicit waits: doing so can produce unpredictable wait durations. Its waiting strategies guide explains the distinction.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #2
Sale
HTML and CSS: Design and Build Websites
  • HTML CSS Design and Build Web Sites
  • Comes with secure packaging
  • It can be a gift option

Cypress

For DOM removal, query the element and assert that it does not exist. Cypress retries the DOM query and assertion until they pass or time out.

cy.get('[data-testid="loading"]').should('not.exist');

Use a visibility assertion instead if the element can remain mounted but must no longer be visible. A nonexistence assertion is specifically about absence; it is not interchangeable with visibility. Cypress documents retrying DOM queries and the .should('not.exist') pattern in its Introduction to Cypress.

Which wait should you use?

Framework Pattern What it establishes Wait behavior
Playwright locator.waitFor({ state: 'hidden' }) or state: 'detached' hidden accepts hidden or removed; detached requires removal Waits for the requested state or times out; succeeds immediately if the state already holds
Selenium Python WebDriverWait(...).until_not(...) The documented visibility example waits until no longer visible Polls the condition until it passes or times out
Cypress cy.get(...).should('not.exist') Asserts that the element is absent from the DOM Retries the query and assertion until they pass or time out

Common failures and fixes

The wait times out, but the page looks finished

Check whether the element is actually hidden or removed. A selector may match a different element, or the app may leave the original mounted but invisible. If invisibility is sufficient, use a hidden or visibility condition; if removal is essential, keep the detached or absence condition and verify the selector targets the element that should be removed.

The test passes while the element is still in the DOM

The test likely checks visibility rather than removal. Use Playwright’s detached state or Cypress’s .should('not.exist') for DOM absence. A condition for no longer visible does not prove that the element was unmounted.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The test is flaky with a fixed sleep

A fixed delay can be too short when a transition takes longer than expected and waste time when it finishes sooner. Replace it with the framework’s state-based wait or retrying assertion. Selenium describes race conditions between the app and automation as a cause of flaky tests in its waiting strategies guide.

Wait duration is unexpectedly long in Selenium

Check whether the test combines implicit and explicit waits. Selenium warns against mixing them because the resulting durations can be unpredictable; use a consistent synchronization strategy.

Rank #4
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • Brand: Wiley
  • Set of 2 Volumes
  • A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers

Or skip the browser setup

If you need an image or PDF of a page rather than an automated assertion about DOM state, ScreenshotNeo provides a one-request screenshot API. It does not replace a test-framework wait: its endpoint captures a page; it does not assert that your app’s element reached a particular state.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

See the ScreenshotNeo API documentation for request options. Before capture it can accept cookie or consent banners and remove known consent platforms, newsletter popups, and chat widgets; each of those steps can be turned off. Bot checks, blank pages, timeouts, failed loads, and cache hits are not billed, and response headers report the page verdict and billing status. Its MCP server provides take_screenshot, get_page_info, and capture_pdf tools for AI agents. The free plan includes 1,000 shots a month with no card; paid plans start at $5 for 3,000 shots.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Sign up for ScreenshotNeo’s free plan to get 1,000 screenshots a month with no card.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Frequently Asked Questions

What is the difference between hidden and detached?

Hidden means the element is not visible; detached means it is no longer in the DOM. Use the latter only when removal itself is what the test must prove.

Does a Playwright hidden wait pass if the element is removed?

Yes. Playwright’s hidden state accepts either hidden or detached; use detached for a strict removal check.

Can Selenium’s until_not visibility wait prove DOM removal?

No. The documented inverse visibility condition establishes that the element is no longer visible, not necessarily that it has been removed.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.