【问题标题】:Cypress + how to store a dynamic web element IDCypress + 如何存储动态 Web 元素 ID
【发布时间】:2021-12-08 19:32:37
【问题描述】:

下午,

我需要能够根据文本匹配找到并存储 ID。

所以我能够找到一些文本并移动到父 div。但我需要一种方法来存储 ID,但只存储数字,因为这将被链接到另一个操作,因为这是由应用程序动态构建的,并且会彻底推迟。

来源。

<div class="link name" role="button" tabindex="0" id="title-6126">
<p>sometext</p>

【问题讨论】:

    标签: cypress


    【解决方案1】:

    你可以这样做:

    cy.get('div[role="button"]').invoke('attr', 'id').then((id) => {
      cy.log(+id.split('-')[1]) //prints 6126 as Number
      cy.wrap(+id.split('-')[1]).as('idNum') //To use later
    })
    
    cy.get('@idNum').then((idNum) => {
      cy.log(idNum) //prints 6126 as Number
      //Access idNum here
    })
    

    现在,如果您想从文本中获取您的 id,您可以:

    cy.contains('sometext')
      .parent()
      .within(() => {
        cy.get('div[role="button"]')
          .invoke('attr', 'id')
          .then((id) => {
            cy.log(+id.split('-')[1]) //prints 6126 as Number
            cy.wrap(+id.split('-')[1]).as('idNum') //To use later
          })
      })
    
    cy.get('@idNum').then((idNum) => {
      cy.log(idNum) //prints 6126 as Number
      //Access idNum here
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-13
      • 1970-01-01
      • 2020-05-08
      • 2013-02-19
      • 2018-09-24
      • 1970-01-01
      • 1970-01-01
      • 2013-08-11
      相关资源
      最近更新 更多