简体   繁体   English

将 HTML 表中的数据存储到二维数组 Selenium Java 中

[英]Storing data from an HTML table into a 2D array Selenium Java

I am trying to store data from an HTML table into a 2D array but I can't seem to figure it out.我正在尝试将 HTML 表中的数据存储到二维数组中,但我似乎无法弄清楚。 My code currently looks like this.我的代码目前看起来像这样。

This is the site where the table is: https://www.w3schools.com/html/html_tables.asp这是表格所在的网站: 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(" ");
  }

And this the current output.这是当前的输出。

Alfreds Futterkiste阿尔弗雷德·富特基斯特
Maria Anders玛丽亚安德斯
Germany德国

Centro comercial Moctezuma Centro commercial 蒙特祖玛
Francisco Chang弗朗西斯科·张
Mexico墨西哥

Ernst Handel恩斯特·亨德尔
Roland Mendel罗兰·孟德尔
Austria奥地利

Island Trading岛屿贸易
Helen Bennett海伦·贝内特
UK英国

Laughing Bacchus Winecellars笑酒神酒窖
Yoshi Tannamuri Yoshi Tannamuri
Canada加拿大

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

I'm trying to get that output to look something like this.我试图让输出看起来像这样。

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

I don't think this has anything to do with Selenium.我认为这与 Selenium 无关。 This is based only on logic.这仅基于逻辑。 Below is the code you should try.以下是您应该尝试的代码。 Let me know if it solves your problem.让我知道它是否能解决您的问题。

    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("]]");
            }       
    }    
}

Required output looks like it is generated using Arrays toString() method.所需的输出看起来像是使用 Arrays toString() 方法生成的。 So you can use a 1D array and using toString() method to generate it directly without worrying about appending spaces and new lines manually.因此,您可以使用一维数组并使用 toString() 方法直接生成它,而无需担心手动添加空格和换行符。

  1. Now you initalize an 1D array to store result of each row.现在你初始化一个一维数组来存储每一行​​的结果。 Create it outside your first loop.在您的第一个循环之外创建它。

     String[] rowResults = new String[rowCount];
  2. You can create another 1D array.您可以创建另一个一维数组。 Place this inside the first loop and outside the second loop.将其放在第一个循环内和第二个循环外。

     String[] columnResults = new String[coulmCount].
  3. As you start reading the elemnts, add it to the columnResults and once you are done with the inner loop.当你开始阅读这些元素时,将它添加到 columnResults 中,一旦你完成了内部循环。 use the below使用以下

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

    Arrays.toString(arr) for an array "arr" will give you [ "a", "b", "c" ] format.数组 "arr" 的 Arrays.toString(arr) 将为您提供 [ "a", "b", "c" ] 格式。

  4. So once you are done with the execution, just run Arrays.toString(rowResults) and you will have the output in required format.因此,一旦您完成执行,只需运行Arrays.toString(rowResults) 即可获得所需格式的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM