Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk4 min

How to Check Whether an Element Exists in Cypress

Cypress retries cy.get() until a matching element exists. Use .should('not.exist') to wait for removal, and distinguish DOM presence from visibility.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use cy.get(selector) to assert that an element exists: Cypress retries the query until it finds a match or the command times out. To wait for an element to disappear, use cy.get(selector).should('not.exist'). An explicit .should('exist') is usually unnecessary because cy.get() already has an implicit existence assertion.

Check that an element exists

Pass a selector to cy.get(). If Cypress finds a matching element, the command yields it; if not, Cypress retries until the configured command timeout is reached.

As an Amazon Associate I earn from qualifying purchases.

// Passes when a matching element exists
cy.get('[data-cy=notice]')

Use a dedicated test attribute such as data-cy when your application supports it. Cypress recommends these selectors because they are less likely to change when styling or visible text changes. See the cy.get() API documentation.

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.

You can also write cy.get('[data-cy=notice]').should('exist'), but the extra assertion is generally redundant.

Check that an element does not exist

Chain .should('not.exist') when the test should wait until an element is absent from the DOM. Cypress retries the query and assertion, making this useful for elements such as loading indicators that are removed after work finishes.

// Passes when the matching element is absent
cy.get('[data-cy=loading-spinner]').should('not.exist')

This checks DOM presence, not whether a person can see the element. If the element must be visible, use .should('be.visible') instead. Cypress distinguishes existence and visibility in its assertions guide.

Choose the assertion that matches the test

Test intent Pattern What it checks
Element should exist cy.get(selector) A matching element exists before the query completes.
Element should be absent cy.get(selector).should('not.exist') The matching element is absent; Cypress retries the assertion.
Element should be visible cy.get(selector).should('be.visible') The matching element satisfies Cypress’s visibility assertion.
Take different action depending on presence Synchronous DOM inspection, only when the state has settled A stable DOM snapshot can guide control flow; a changing snapshot cannot reliably do so.

Use retries instead of one-time inspection

Cypress retries queries and chained .should() assertions while waiting for the condition to pass. A .then() callback, by contrast, runs once after its preceding command yields. Do not use it as a substitute for a retrying assertion when content is still loading or the DOM may change. See the cy.should() documentation.

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

The default wait comes from defaultCommandTimeout. For an individual query, you can provide a timeout option:

cy.get('[data-cy=notice]', { timeout: 10000 })

Set a longer timeout only when the application genuinely needs more time; an overly generous timeout can make failures slower to diagnose. The cy.get() documentation describes the command option and retry behavior.

Be careful with conditional checks

Checking the DOM and then branching can make a test flaky if the application may still render or update asynchronously. Cypress’s conditional testing guide says DOM-based branching is appropriate only when the state is known to have settled and cannot change. Prefer making the application deterministic or using another stable source of truth when possible.

A negative assertion also cannot prove that a transient element appeared earlier: .should('not.exist') may pass before that element ever enters the DOM. If the order matters, assert the intermediate state first, then assert its removal. Cypress demonstrates this sequencing for a saving message in the cy.contains() API documentation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
// First establish that the message appeared, then that it disappeared
cy.contains('Saving...').should('be.visible')
cy.contains('Saving...').should('not.exist')

Limits and common failures

  • No matching element: Cypress keeps retrying, then fails when the timeout expires. Check the selector, confirm the relevant page state, and use a stable test attribute where available.
  • Assertion passes too early: A negative assertion can pass before a transient element appears. Assert the expected earlier state first when sequence matters.
  • Flaky conditional branch: The DOM may still be changing when inspected. Wait for a reliable settled state or avoid DOM-based branching.
  • Element is present but not visible: Existence alone does not establish visibility. Assert be.visible when visibility is the requirement.
  • Element is inside an iframe: cy.get() searches the application document and does not descend into iframe documents. Handle the iframe using an approach supported by your Cypress setup; the standard query does not search inside it.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup

If your task is to capture a page rather than test its DOM, ScreenshotNeo offers a screenshot API and MCP server. Its one-call API example is:

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. Cookie banners, newsletter popups and chat widgets are removed before capture; bot checks, blank pages and failed loads are not billed. Its MCP server lets AI agents take screenshots. The free plan includes 1,000 screenshots a month with no card, and paid plans start at $5 for 3,000. Sign up for free.

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.