【问题标题】:i couldn't type in an element using cypress我无法使用 cypress 输入元素
【发布时间】:2021-12-24 14:00:57
【问题描述】:

我正在尝试使用 cypress 在注册表单中填写密码字段,但它不适用于我 我的代码:

describe("SignUP", function(){

it('OpenSignuoForm',function(){
cy.visit('https://mawadda-eg.com')
cy.get('.tt-tools-button').click();
cy.get('#header > div > nav > div.tt-menu-collapse.tt-submenu-dark > ul > li:nth-child(2) > ul > li:nth-child(2) > a').click();
cy.get('#user_name').type('automation');
cy.get('#e_mail').type('moatazqc+100@gmail.com');
cy.get('#password').type('test');
})    
})

【问题讨论】:

  • 这里是我得到的错误 Timed out retrying after 4000ms: cy.type() failed because this element is not visible:
  • 元素是否可见?你如何验证它实际上是可见的并且你的选择器工作?考虑到“OpenSignuoForm”已经包含一个“,您的选择器中是否有错别字

标签: cypress


【解决方案1】:

在检查附加的网站后,它没有工作,因为该 id 重复,使用另一个 css 选择器,或者使用 XPath 定位器与此定位器 (//input[@id='password'])[1]

在 cypress 中使用 XPath 定位器的详细解答。

在 cypress 中,默认的定位器策略是使用 CSS 选择器,使用普通的 cy.get(),但如果我们愿意,我们也可以使用 XPath 插件 https://github.com/cypress-io/cypress-xpath 定位元素。

如何安装和使用

  1. 用纱线安装它yarn add cypress-xpath --dev
  2. 然后在项目支持文件中包含cypress/support/index.js as
require('cypress-xpath')
  1. cy.xpath() 的任何it 测试中使用它,您可以像链接cy.get() 一样链接它

因此,您可以在问题中的元素上找到并键入的最终代码应该是

cy.xpath("(//input[@id='password'])[1]").type("test");

【讨论】:

  • 请提供确切的命令
  • 如果你想使用 XPath ,你可以先安装 xpath 插件,然后使用定位器 (//input[@id='password'])[1] ,让我在我的答案中添加详细信息
  • 谢谢亲爱的。它现在对我有用
  • @MoatazSelim 感谢将我的答案标记为正确答案,因为它确实有帮助:D
猜你喜欢
  • 1970-01-01
  • 2020-03-31
  • 1970-01-01
  • 2022-01-20
  • 1970-01-01
  • 2022-06-14
  • 2023-03-18
  • 2020-04-30
  • 2021-12-07
相关资源
最近更新 更多