Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Got "cy.click failed because the page updated while this command was executing" error

Tags:

cypress

I wrote this code to click on the first option of a dropdown and ran it in Cypress

it('Choosing tag', () => {
    cy.get('[data-appearance="textfield"]').eq(1).click();
    cy.get('[role="option"]').eq(0).as('option').should('be.visible');
    cy.get('@option').then(($option) => {
      cy.wrap($option).click();
    });
  });

But on the cy.click phase I got this error

Timed out retrying after 4050ms: cy.click() failed because the page updated while this command was executing. Cypress tried to locate elements based on this query:

<button.ng-star-inserted>

We initially found matching element(s), but while waiting for them to become actionable, they disappeared from the page. Common situations why this happens: Your JS framework re-rendered asynchronously Your app code reacted to an event firing and removed the element

You can typically solve this by breaking up a chain. For example, rewrite:

cy.get('button').click().click() to cy.get('button').as('btn').click() cy.get('@btn').click()

As you can see in my code, I followed recommendations from the error text but it didn't help. When I visit the page as a user, the required element is clickable.

I hope there are ways to solve this. Thank you.

like image 566
O_Divano Avatar asked Sep 12 '25 08:09

O_Divano


1 Answers

To stop the test waiting for actionability, you can add the {force:true} option.

This means that you will be using the old version of the button, but that may be the quickest way around your problem.

There is no need to then() and wrap(), it does nothing.

cy.get('[data-appearance="textfield"]').eq(1).click();
cy.get('[role="option"]').eq(0).as('option').should('be.visible');

cy.get('@option').click({force:true})

In a real-world test you will look for

  • an artifact on the page (e.g cy.contains(some-page-text))
  • or a network intercept (e.g cy.intercept('some-api-response'))

to be sure the page is stable before clicking the button.

Since there is no info about your page, I can't give you a specific answer about that.

like image 189
Geboren Avatar answered Sep 15 '25 05:09

Geboren



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!