【问题标题】:Cypress find element from multiple elements based on text and click on itCypress 根据文本从多个元素中查找元素并单击它
【发布时间】:2020-03-14 23:02:00
【问题描述】:

Cypress:我想从基于文本的元素列表中找到一个元素,并希望点击该元素

        cy.get('partie-room-list-item > div > div.content-block > span.partie-title').each(($elm , i) => {
            cy.get($elm).invoke('text').then((text) => {
            if (text === 'textinelement') {
                expect(text.trim()).equal('textinelement').click();
              cy.log(text)                  
            } 
          })




    });

cypress find element and click https://www.screencast.com/t/p0rL6qexD5

【问题讨论】:

  • 请看链接中的图片
  • 您找到解决方案了吗?
  • 是的 Maccurt 非常感谢我将如何正确测试这种情况请screencast.com/t/EIx6rbKnX4k8 选项需要在单击时关闭
  • 如果答案是由下面的人提供的,请点赞或标记为答案。我会看看你的其他问题..谢谢

标签: list find element each cypress


【解决方案1】:

您能否尝试以下代码并告诉我结果。您可以使用 .text() 从 span 元素中获取文本。

cy.get('partie-room-list-item > div > div > .partie-title').each(ele => {
           const eleText = "cypresssendfollowRequest";
            if (ele.text() === eleText) {
               ele.click();
           }
      });

【讨论】:

    【解决方案2】:

    我只是在这里猜测。如果您发布您的 HTML,我可能会为您提供帮助

    cy.get('.partie-title').contains('textinelement').click() 
    

    看你的图片会是这样的

    cy.get('.partie-title').contains('cypresssendfollowRequest').click() 
    

    【讨论】:

      【解决方案3】:
      it("This test is for selecting a single element based on text",()=>{
          cy.visit('http://automationpractice.com/index.php')
          cy.get("[title='Women']").each(($element, index)=>{
              console.log("Object is "+ $element.text())
              if($element.text()=='Women'){
                  cy.get($element).click()
              }
          })
      }) 
      

      it("This test is for selecting a single element based on text",()=>{
              cy.visit('http://automationpractice.com/index.php')
              cy.get("[title='Women']").contains('Women').click()                 
      })
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-25
        相关资源
        最近更新 更多