简体   繁体   中英

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

I have been trying a task that there is the table which contains three rows and seven columns and each column contains an integer number and the aim is to read the table(DOM) by Selenium and extract each row each column integer value and has to create an array with all these integer numbers. As finally, I do have three arrays(a[],b[],c[]) with every seven elements.if anyone has a good start to implement this task will be appreciated.

<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 add below dependency:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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