【问题标题】:Storing data from an HTML table into a 2D array Selenium Java将 HTML 表中的数据存储到二维数组 Selenium Java 中
【发布时间】:2017-07-14 02:07:17
【问题描述】:

我正在尝试将 HTML 表中的数据存储到二维数组中,但我似乎无法弄清楚。我的代码目前看起来像这样。

这是表所在的站点:https://www.w3schools.com/html/html_tables.asp

@Test
public void checkTable() {

  // Number of Rows in table
  int rowCount = driver.findElements(By.xpath("//*[@id='customers']/tbody/tr")).size(); 
  System.out.println("The total number of rows is " + rowCount);

  // Number of Columns in table
  int colCount = driver.findElements(By.xpath("//*[@id='customers']/tbody/tr[1]/th")).size();
  System.out.println("The total number of columns is " + colCount);

  // Dynamic X-Path

  String firstX = "//*[@id='customers']/tbody/tr[";
  String secondX = "]/td[";
  String thirdX = "]";

  for(int i=2; i <= rowCount; i++) {
      for(int j=1; j <= colCount; j++) {
          String completeX = firstX + i + secondX + j + thirdX;
          String tableData = driver.findElement(By.xpath(completeX)).getText();
          System.out.println(tableData + "   ");
      }
      System.out.println(" ");
  }

这是当前的输出。

阿尔弗雷德·富特基斯特
玛丽亚·安德斯
德国

Centro comercial Moctezuma
弗朗西斯科·张
墨西哥

恩斯特·亨德尔
罗兰·孟德尔
奥地利

岛屿贸易
海伦·贝内特
英国

笑着的巴克斯酒窖
Yoshi Tannamuri
加拿大

Magazzini Alimentari Riuniti
乔瓦尼·罗维利
意大利

我试图让输出看起来像这样。

[[Alfreds Futterkiste,Maria Anders,德国],[Centro comercial Moctezuma,Francisco Chang,墨西哥]]...等。

【问题讨论】:

    标签: java arrays selenium qa


    【解决方案1】:

    我认为这与 Selenium 无关。这仅基于逻辑。 以下是您应该尝试的代码。让我知道它是否能解决您的问题。

        for(int i=2; i <= rowCount; i++) {
            if(i==2)
            {
                System.out.print("[");
            }
    
            for(int j=1; j <= colCount; j++) {
                String completeX = firstX + i + secondX + j + thirdX;
                String tableData = driver.findElement(By.xpath(completeX)).getText();
    
                if (j==1)
                {
                System.out.print("[");
                }
    
                if (j==colCount)
                {
                    System.out.print(tableData);
                }
                else
                {
                    System.out.print(tableData + ',');
                }
    
                if (j==colCount && i!=rowCount)
                {
                    System.out.print("],");
                }
    
                if (j==colCount && i==rowCount)
                {
                    System.out.print("]]");
                }       
        }    
    }
    

    【讨论】:

    • 嗨!它似乎有效,但我实际上想将信息存储在二维数组中,而不仅仅是控制台记录看起来像数组的内容。我对 Java 还是很陌生。请帮忙。
    • @niko - 你还需要括号'['和逗号','吗?
    【解决方案2】:

    所需的输出看起来像是使用 Arrays toString() 方法生成的。因此,您可以使用一维数组并使用 toString() 方法直接生成它,而无需担心手动添加空格和新行。

    1. 现在你初始化一个一维数组来存储每一行​​的结果。在您的第一个循环之外创建它。

      String[] rowResults = new String[rowCount];
      
    2. 您可以创建另一个一维数组。将其放在第一个循环内和第二个循环外。

      String[] columnResults = new String[coulmCount].
      
    3. 当你开始阅读元素时,将它添加到 columnResults 中,一旦你完成了内部循环。使用下面的

      String columnOutput = Arrays.toString(columnResults);
      rowResults.add(columnOutput);
      

      Arrays.toString(arr) 数组 "arr" 将为您提供 [ "a", "b", "c" ] 格式。

    4. 所以一旦你完成了执行,就运行 Arrays.toString(rowResults),您将获得所需格式的输出。

    【讨论】:

    • 我看到编译错误。 Cannot invoke add(String) on the array type String[] 发生在 rowResults.add(columnOutput); 上。有什么建议吗?
    猜你喜欢
    • 1970-01-01
    • 2014-01-04
    • 1970-01-01
    • 1970-01-01
    • 2012-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多