【问题标题】:CypressError: Timed out retrying: cy.click() failed because this element is detached from the DOMCypressError:重试超时:cy.click() 失败,因为此元素已与 DOM 分离
【发布时间】:2019-11-13 08:59:03
【问题描述】:

一旦收到后端响应,下拉菜单基本上需要很长时间才能自行加载。如果我等待大约 8 秒,那么它可以工作。但是,不想在这里对等待进行硬编码。知道可能出了什么问题吗?我也无法识别 css。

cy.get('input').last().type(`{selectall}${value}`);
        cy.get('mat-option > span').then(option => {
            if (option.get(0).textContent === 'Loading...') {
                cy.wait(5000);
            }
        });   

cy.containsCaseInsensitive(value, 'mat-option').first().scrollIntoView().debug().click();

错误日志 -

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

<mat-option _ngcontent-gcj-c21="" class="mat-option ng-star-inserted" role="option" ng-reflect-value="[object Object]" tabindex="0" id="mat-option-104" aria-disabled="false" style="">...</mat-option>

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

The previous command that ran was:

  > cy.debug()

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

【问题讨论】:

    标签: angular cypress


    【解决方案1】:

    我确实遇到了同样的问题 让柏树等待一段时间直到页面加载 解决了分离元素的问题

    例子

    cy.wait(3000)
    cy.get(growth.tpsl_input_sessionName).clear().type(sessionName);
    

    【讨论】:

      【解决方案2】:

      为了等待网络响应,您可以为网络请求设置别名并等待它们。

      在此处查看赛普拉斯的文档:https://docs.cypress.io/guides/guides/network-requests.html#Flake

      cy.route('/search*', [{ item: 'Book 1' }, { item: 'Book 2' }]).as('getSearch')
      
      // our autocomplete field is throttled
      // meaning it only makes a request after
      // 500ms from the last keyPress
      cy.get('#autocomplete').type('Book')
      
      // wait for the request + response
      // thus insulating us from the
      // throttled request
      cy.wait('@getSearch')
      

      点击上面的链接可以在文档中找到更多示例。

      【讨论】:

        【解决方案3】:

        我遇到了同样的问题,但你可以解决它

        cy.get('.mySelector').should('be.visible').click({force:true});

        【讨论】:

        • 疯了...谢谢伙计
        • 哦不,难以置信..不敢相信我找到了这个答案。非常感谢。你真的为我解决了一个巨大的巨大问题。我被这个问题困了将近一个星期。然后我遇到了这个答案。这几乎就像节食后吃biryani。你可以想象我得到的幸福。感谢您再次发布此答案,先生。尊重!
        【解决方案4】:

        也许是一个迟到的答案,但对其他有同样问题的人有用,有时使用 Cypress jQuery 包装器可能是个好主意。

        Cypress.$('.mySelector').trigger('click');
        

        【讨论】:

        • 谢谢!这是我发现的唯一可行的解​​决方案,可以单击 Magento 结帐页面中的运输方式单选按钮。
        【解决方案5】:

        你可以这样试试

        cy.get('input').last().type(`{selectall}${value}`);
                cy.get('mat-option > span').then(option => {
                    //if (option.get(0).textContent === 'Loading...') {
                        //cy.wait(5000);
                    //}
        
                    cy.containsCaseInsensitive(value, 'mat-option').first().scrollIntoView().debug().click();
                });
        

        【讨论】:

        • 已经试过了。顺便说一句,你为什么觉得它会这样工作,只是好奇。感谢您尝试提供帮助。它不会这么快加载下拉列表,后端查询需要时间来返回值。
        • 您可以传递 cy.wait('@getBooks'),而不是在 wait 中传递数字。请查看docs.cypress.io/guides/guides/network-requests.html#Waiting
        • 它是一个通用代码,每次可以是不同的xhr,因此不能使用@getBooks
        猜你喜欢
        • 2022-11-23
        • 1970-01-01
        • 2022-08-18
        • 1970-01-01
        • 1970-01-01
        • 2013-07-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多