question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Re-query elements that are found 'detached' from the DOM

See original GitHub issue

Current status

Update 12/06/2022: This work as been complete and will be released in Cypress v12 today.

This is currently in progress. See https://github.com/cypress-io/cypress/issues/7306#issuecomment-1182059194, https://github.com/cypress-io/cypress/issues/7306#issuecomment-1182427284 and https://github.com/cypress-io/cypress/issues/7306#issuecomment-1188245931 for a brief description of the plan and updates on current status.

Current behavior:

Currently when Cypress queries an element that was passed from a parent element (say in .get(), .click() or .find()), if the element at some point becomes detached from the DOM, we throw an error:

CypressError: cy.click() failed because this element is detached from the DOM.

<element>

Cypress requires elements be attached in the DOM to interact with them.

The previous command that ran was:

  cy.get()

This DOM element likely became detached somewhere between the previous and current command.

Common situations why this happens:
  - Your JS framework re-rendered asynchronously
  - Your app code reacted to an event firing and removed the element

You typically need to re-query for the element or add 'guards' which delay Cypress from running new commands.

https://on.cypress.io/element-has-detached-from-dom
Screen Shot 2020-11-02 at 10 27 54 AM

Desired behavior:

Really users just want to re-query the DOM for the previously found element and click on the new element. Because it’s more often than not that the framework underneath has created a completely new DOM element and inserted it into the DOM.

The proposal is to re-query the previously found element and continue the command with any newfound elements. Perhaps this could be an option passed to the command to allow this or not.

Reproducible Example

Provided in https://github.com/cypress-io/cypress/issues/9032

index.html

<form>
    <select onchange="document.forms[0].submit()">
        <option value="1">First</option>
    </select>
    <input />
</form>

spec.js

it('this will fail but should not', () => {
    cy.visit('index.html')
    cy.get("select").select("First")
    // adding any wait time will make it pass
    // cy.wait(0)
    cy.get("input").type("Hallo")
})

Related issues

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:545
  • Comments:315 (60 by maintainers)

github_iconTop GitHub Comments

206reactions
JonDumcommented, May 16, 2022

@TomaszG A plugin, a wait, an additional check in a loop, an additional selector. All I can say is… yikes. Cypress is kinda shitting the bed here.

168reactions
pmaouicommented, Jun 6, 2020

All solutions here are at best hacky workarounds. The Cypress spirit is to interact with UIs like an user would do. This issue is indeed very technical and obviously an user is not supposed to know if an element is render one or multiple times, so as the test writer.

IMO, This re-query should be done systematically, without option to enforce the Retry-ability core-concept of Cypress.

For now, my only not-100%-safe workaround is a cy.wait(xxx) before any risky selector. It goes against core-concepts of this project.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Solving The Element Is Detached From DOM Error In Cypress
The "element is detached from DOM" is a sign of a problem, not the problem itself! There is a variety of ways the...
Read more >
Why is Cypress saying my element is detached after just ...
Getting this error means you've tried to interact with a "dead" DOM element - meaning it's been detached or completely removed from the...
Read more >
Do Not Get Too Detached - Cypress
In this blog post I will go through a scenario showing this problem and show how to fix it.
Read more >
your js framework re-rendered asynchronously - your app ...
debug() This DOM element likely became detached somewhere between the previous and current command. Common situations why this happens: - Your JS framework...
Read more >
Debug DOM memory leaks with the Detached Elements tool
Use the Detached Elements tool to find detached elements that the browser cannot garbage-collect, and locate the JavaScript object that is ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found