【问题标题】:How to get row counts on a dynamic table if <tr> numbers keep changing如果 <tr> 数字不断变化,如何获取动态表的行数
【发布时间】:2021-09-29 22:43:16
【问题描述】:

作为测试人员,我需要验证动态表上的总行数。我发现 HTML 最多只能显示 22 个&lt;tr&gt;。我实际上在表格上有 50 行,但是当我在自动化上运行以下代码时,它只返回 22。当我向下滚动表格上的行时,HTML 上的 &lt;tr&gt; 行不断变化,但从来没有 50 行。它使我的测试失败。

int iRowsCount = driver.findElements(By.XPath("/html/body/app-root/div/***/div[1]/table/tbody/tr")).Count;

如何获得正确的行数?

HTML结构是这样的,

<div class=****>
 <table ********>
   <thead *****>...</thead>
   <tbody *****>
     <tr ****>.... </tr>
     <tr ****>.... </tr>
     <tr ****>.... </tr>
     <tr ****>.... </tr>
     <tr ****>.... </tr>
   </tbody>
   <tfoot ****>...</tfoot>
 </table>
</div>

【问题讨论】:

  • 可以分享网址吗?
  • 我遇到了类似的问题。在我的情况下,行也是动态的,最多显示 60 个 。滚动时,将加载新行( )。所以我猜你还需要使用 scrollInView javascript函数来加载剩余的行。如果有意义,我可以发布答案。
  • 当我滚动时,新行被加载但旧行消失了。和你的一样吗?如果您可以发布答案,那就太好了。非常感谢。
  • @Cissi 我错过了你的评论。如果您仍在寻找解决方案,我可以发布答案。

标签: html selenium automated-tests ui-automation dynamic-tables


【解决方案1】:

请尝试下面的代码并告诉我。

boolean element = scrollIntoListView("max", false, webElement);
     if(element == true){
      /*Perform actions on element*/
     }

在任何你想滚动的地方调用scrollIntoListView方法并寻找所需的元素。

    /*
     * This method is to scroll into list for particular element
     * 
     * @Params - List number, true or false, webElement.
     * 
     * Example#1: The list is having 60 elements and if you want to scroll till last
     * element and you want that element to be displayed on bottom of page then the
     * parameters should be like this. scrollIntoListView(max, false, element)
     *
     * Example#2: The list is having 60 elements and if you want to scroll till 30th
     * element and you want that element to be displayed on top then the parameters
     * should be like this. scrollIntoListView(30, true, element)
     */

    public static boolean scrollIntoListView(String listNumber, boolean topOrBottom, WebElement element) {
        WebDriver driver = (WebDriver) Driver.driverMap.get("WEBDRIVER");

        /*Here waitForVisibilityOfWebElement method returns an element if it is visible on DOM else null value will be returned.*/
        WebElement element = waitForVisibilityOfWebElement("element");
        if (element != null) {
            return true;
        } else {
            JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver;
            int tillElement = 0;
            List<WebElement> optionList = null;

            if (listNumber.equalsIgnoreCase("max")) {
                optionList = driver.findElements(By.xpath(
                        "xPath to list of elements"));
                tillElement = (optionList.size() - 1);
            } else {
                tillElement = Integer.valueOf(listNumber);
            }
            javascriptExecutor.executeScript("arguments[0].scrollIntoView(" + topOrBottom + ");",
                    optionList.get(tillElement));

            /*Once you scroll down to the bottom or top of the element then again look for that element.*/
            element = waitForVisibilityOfWebElement("element");
            if (element != null) {
                return true;
            }
        }
        return false;
    }

注意:您必须编写自己的waitForVisibilityOfWebElement 方法。

【讨论】:

  • 感谢您的回答。但是,我需要返回表中所有行的计数,而不是查找特定行。向下滚动到底部不会给我一个解决方案,因为顶部的行在滚动过程中无论如何都会消失。
  • @Cissi 你的具体情况是什么?你想得到所有的行吗?或尝试对特定行执行操作?请张贴HTML代码并详细说明情况。
  • 我正在测试一个表格的下拉列表。它用于显示您希望在表格上看到多少个结果。当我选择 50 或 100 时,我应该看到有 50 或 100 行,但我永远看不到那么多,因为屏幕最多只显示 22 行。上面粘贴了 HTML 结构。
  • 在 UI 中,您一次可以看到 22 行,当您向下滚动时,将加载剩余的行。对吗?
  • 听起来像一个解决方案。我会试试看。谢谢,南丹。
猜你喜欢
相关资源
最近更新 更多
热门标签