首页 > 解决方案 > 无法使用 cypress 访问 html 元素

问题描述

我正在尝试使用 cypress 测试登录功能,但由于某种原因,我无法访问输入文本框。它显示 Timed out retrying after 4000ms: Expected to find element: runner container,但从未找到它。

describe("User authentication", () => {
    beforeEach(() => {
      cy.visit("https://lmflf.com");
     
    });
    it("it takes to the correct page", () => {
   
      cy.get('[data-field=password]')
      
    });
    
  });

标签: javascriptautomationcypress

解决方案


我可以在您的网页中看到一个影子 DOM,在这种情况下您必须使用.shadow()( Cypress Docs )。您的代码将类似于:

cy.get('view-login').shadow().find('input[data-field="username"]').type('username', {force: true})
cy.get('view-login').shadow().find('input[data-field="password"]').type('password', {force: true})
cy.get('view-login').shadow().find('input[value="personal"]').click()
cy.get('view-login').shadow().find('.ViewLogin__button').click()

推荐阅读