首页  >  问答  >  正文

使用each()中的invoke()访问href属性 Cypress

我是 Cypress 的新手,我尝试使用 invoke() 从组中访问每个 div 标签的 href 属性,但它给出了错误。有人可以建议你如何做吗?

cy.get('.bms-scoreboard__game-tile--mls').each(($el,index,$list) => {
            $el.get('a')
                .invoke('attr','href')
                .then(href => {
                    cy.request(href)
                        .its('status')
                        .should('eq',200)
                })
        })

P粉231112437P粉231112437312 天前445

全部回复(2)我来回复

  • P粉276577460

    P粉2765774602023-12-13 00:52:13

    $el 是一个 JQuery 元素,而不是它本身在 Cypress 链中。您需要使用 cy.wrap() 在 Cypress 链中使用它。

    cy.get('.bms-scoreboard__game-tile--mls').each(($el,index,$list) => {
                cy.wrap($el)
                    .get('a')
                    .invoke('attr','href')
                    .then(href => {
                        cy.request(href)
                            .its('status')
                            .should('eq',200)
                    })
            })
    

    回复
    0
  • P粉359850827

    P粉3598508272023-12-13 00:36:38

    我认为 .get() 不合适 - 它仅适用于 <body>,不适用于每个 '.bms-scoreboard__game-tile--mls'

    尝试使用 .find() 代替

    使用 jQuery 运算符

    cy.get('.bms-scoreboard__game-tile--mls')
      .each(($el,index,$list) => {
        const href = $el.find('a').attr('href')
        cy.request(href)
          .its('status')
          .should('eq', 200)
      })
    })
    

    或与赛普拉斯运营商合作

    cy.get('.bms-scoreboard__game-tile--mls')
      .each(($el,index,$list) => {
        cy.wrap($el).find('a')
          .invoke('attr','href')
          .then(href => {
            cy.request(href)
               .its('status')
               .should('eq',200)
          })
      })
    })
    

    或将“查找”移至第一个选择器

    cy.get('.bms-scoreboard__game-tile--mls a')
      .each($a => {
        const href = $a.attr('href')
        cy.request(href)
          .its('status')
          .should('eq', 200)
      })
    })
    

    回复
    0
  • 取消回复