繁体   English   中英

读取 DOM 并使用 selenium 为每一行创建一个数组数据结构

[英]Read the DOM and create an array data structure for each rows using selenium

我一直在尝试一项任务,该表包含三行七列,每列包含一个整数,目的是通过 Selenium 读取表(DOM)并提取每行每列整数值,并且必须创建一个包含所有这些整数的数组。 最后,我确实有三个数组(a[],b[],c[]),每七个元素。如果有人有一个好的开始来实现这个任务,我们将不胜感激。

<tr style="border-bottom: 1px solid rgb(224, 224, 224); color: rgba(0, 0, 0, 0.87); height: 48px;">
  <td data-test-id="array-item-1-0" style="padding-left: 24px; padding-right: 24px; height: 48px; 
     text-align: left; font-size: 13px; overflow: hidden; white-space: nowrap; text-overflow: 
     ellipsis; background-color: inherit;">23</td>
  <td data-test-id="array-item-1-1" style="padding-left: 24px; padding-right: 24px; height: 48px; 
     text-align: left; font-size: 13px; overflow: hidden; white-space: nowrap; text-overflow: 
     ellipsis; background-color: inherit;">50</td>
  <td data-test-id="array-item-1-2" style="padding-left: 24px; padding-right: 24px; height: 48px; 
     text-align: left; font-size: 13px; overflow: hidden; white-space: nowrap; text-overflow: 
     ellipsis; background-color: inherit;">63</td>
  <td data-test-id="array-item-1-3" style="padding-left: 24px; padding-right: 24px; height: 48px; 
     text-align: left; font-size: 13px; overflow: hidden; white-space: nowrap; text-overflow: 
     ellipsis; background-color: inherit;">90</td>
  <td data-test-id="array-item-1-4" style="padding-left: 24px; padding-right: 24px; height: 48px; 
     text-align: left; font-size: 13px; overflow: hidden; white-space: nowrap; text-overflow: 
     ellipsis; background-color: inherit;">10</td>
  <td data-test-id="array-item-1-5" style="padding-left: 24px; padding-right: 24px; height: 48px; 
     text-align: left; font-size: 13px; overflow: hidden; white-space: nowrap; text-overflow: 
     ellipsis; background-color: inherit;">30</td>
  <td data-test-id="array-item-1-6" style="padding-left: 24px; padding-right: 24px; height: 48px; 
     text-align: left; font-size: 13px; overflow: hidden; white-space: nowrap; text-overflow: 
     ellipsis; background-color: inherit;">155</td>
  <td data-test-id="array-item-1-7" style="padding-left: 24px; padding-right: 24px; height: 48px; 
     text-align: left; font-size: 13px; overflow: hidden; white-space: nowrap; text-overflow: 
     ellipsis; background-color: inherit;">23</td>
  <td data-test-id="array-item-1-8" style="padding-left: 24px; padding-right: 24px; height: 48px; 
     text-align: left; font-size: 13px; overflow: hidden; white-space: nowrap; text-overflow: 
     ellipsis; background-color: inherit;">18</td>
</tr>
import org.apache.commons.lang3.ArrayUtils;

WebElement table_or_tbody = driver.findElement(<locator of your table or  tbody>);

// find all table rows
List<WebElement> rows = table_or_tbody.findElements(By.cssSelector("tr"));

List<String[]> table_data = new ArrayList<String[]>();

for(WebElement row:rows) {

    // find all cells of each table row
    List<WebElement> cells = row.findElements(by.cssSelector("td"));
    String[] texts = new String[0];

    // read text of each cell
    for(WebElement cell:cells) {
        texts = ArrayUtils.add(texts, cell.getText().trim());
    }

    table_data.add(texts);
}

String[] a = table_data.get(0);
String[] b = table_data.get(1);
String[] c = table_data.get(2);

Maven pom.xml 添加以下依赖项:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.4</version>
</dependency>

暂无
暂无

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

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