簡體   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