I wrote the code to read each li and I warp it up to get each link. And I tried to visit each link in the loop and verify some element which displayed on each page. However, after the new page loads, cypress throws me an error of DOM detach.
I tried to loop and click each link in many ways but I have no luck to make it successfully.
I tried this.
it('test', () => {
cy.visit('https://www.cypress.io/')
cy.get('li.header__NavItem-xi2ch0-6').each(($el, index, list) => {
cy.wrap($el).children().click()
})
})
And this.
it('test', () => {
cy.visit('https://www.cypress.io/')
cy.get('li.header__NavItem-xi2ch0-6').each(($el, index, list) => {
cy.wrap($el).children().and('have.attr', 'href').then((href) => {
cy.log(href)
cy.visit('https://www.cypress.io/' + href)
})
})
})
However, cypress throws me an error of DOM detach.
CypressError: cy.children() failed because this element is detached from the DOM.
...
Cypress requires elements be attached in the DOM to interact with them.
The previous command that ran was:
cy.wrap()
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.