【问题标题】:Accessing text of the element using invoke() in each(). Cypress在 each() 中使用 invoke() 访问元素的文本。柏
【发布时间】:2021-07-01 18:40:19
【问题描述】:

想要访问元素组中的文本。 这种方法不起作用,跑步者给出一个错误,说调用不是函数

cy.get('div[class^="lalala"]')
          .each(function($sec, i, $sects)  {
            $sec.find('header[class^="tatata"]')
            .invoke('text').then((text) => {
             let secText = text
             cy.log(secText);
         });
        })

但如果没有each(),当我访问任何元素时它都可以工作:

cy.get('div[class^="lalala"]').first()
          .find('header[class^="tatata"]')
          .invoke('text')
          .then((text) => {
             let secText = text
             cy.log(secText);
         });
        })

我该如何处理?

【问题讨论】:

    标签: cypress each


    【解决方案1】:

    $sec 是一个包装好的 jQuery 元素,$sec.find() 也返回一个 jQuery 元素。您必须使用cy.wrap 调用invoke 就可以了。

    cy.get('div[class^="lalala"]').each(function ($sec, i, $sects) {
      cy.wrap($sec.find('header[class^="tatata"]'))
        .invoke('text')
        .then((secText) => {
          cy.log(secText)
        })
    })
    

    【讨论】:

      猜你喜欢
      • 2017-08-21
      • 2014-12-16
      • 1970-01-01
      • 2021-01-27
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多