【问题标题】:How to get the total number of Rows in a table | Cypress如何获取表中的总行数 |柏
【发布时间】:2020-11-15 02:15:52
【问题描述】:
  1. 我有一个有 N 行的表。如何获取表格中的总行数?
  2. 我搜索一个名称,该特定名称位于第 X 行中,我如何获取该特定行的值。

【问题讨论】:

    标签: cypress webautomation


    【解决方案1】:

    您可以使用.find 解决您的两种情况。

    获取表格行数:

      cy.get("#tableID")
        .find("tr")
        .then((row) => {
          //row.length will give you the row count
          cy.log(row.length);
        });
    

    要获取特定行的值(索引),您可以执行以下操作。

      cy.get("#Table Id")
        .find("tr")
        .then((rows) => {
          rows.toArray().forEach((element) => {
            if (element.innerHTML.includes("Your Value")) {
            //rows.index(element) will give you the row index
              cy.log(rows.index(element));
            }
          });
        });
    

    附加提示:如果要选择包含值的特定表格单元格,可以这样做:

      cy.get("#customers").find("tr").find("td").contains("Germany");
    

    注意:获取表格行索引可以有许多其他的替代方法。希望你能在旅途中弄清楚它们。

    【讨论】:

    • 感谢您的回复,穆迪塔佩雷拉。这有帮助。
    • 太棒了!很高兴它有帮助:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-05
    • 2012-08-06
    • 1970-01-01
    • 2019-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多