简体   繁体   中英

Extracting a table data from Webpage using Selenium webdriver

Am using Selenium webdriver (in Eclipse) to automate a web app however now the requirement is to capture a table data displayed in one of the html page. I tried with the solutions given here , here and few other websites however our webpage seems to have bit different way of displaying table 在此处输入图片说明

Tried to get the values using div class names as String Text = driver.findElements(By.xpath("//div[@class='ag-row ag-row-even ag-row-level-0']//tr")).get(0).getText(); however it did not work, Index out of bounds exception is thrown

From what I see, you seem to have a custom table built. And from the HTML excerpt in the attached image, the structure is something like:

<div class="ag-body-container" ...>
    <div class="row_1_class" ...>
        <div class="column_1_class" ...>
        <div class="column_2_class" ...>
        <div class="column_3_class" ...>
        <div class="column_4_class" ...>
        ... etc
    <div class="row_2_class" ...>
        <div class="column_1_class" ...>
        <div class="column_2_class" ...>
        <div class="column_3_class" ...>
        <div class="column_4_class" ...>
        ... etc

But your xPath is assuming that you have table rows (and I'm guessing maybe table cells afterwards):

By.xpath("//div[@class='ag-row ag-row-even ag-row-level-0']//tr")

causing your array to be empty (funny enough that you don't get a NoSuchElement exception, perhaps there are some tr tags somewhere in your html tree).

Now, I'm not sure what data you're trying to extract from that table, but your best try would be to get all the rows, based on the class attribute and for each row to get all columns data based on, again, class attribute (or you can even use the col attribute for these).

EDIT: To get all the elements, you could take all rows, and afterwards for each row get all column data:

//Get all the rows from the table
List<WebElement> rows = driver.findElements(By.xpath("//div[contains(@class, 'ag-row')));

//Initialize a new array list to store the text
List<String> tableData = new ArrayList<String>();

//For each row, get the column data and store into the tableData object
for (int i=0; i < rows.size(); i++) {
    //Since you also have some span tags inside (and maybe something else)
    //we first get the div columns
    WebElement tableCell = rows.get(i).findElements(By.xpath("//div[contains(@class, 'ag-cell')]"));
    tableData.add(tableCell.get(0).getText());
}

You could also store your data into bi-directional array (or any of this sort) and afterwards access the data based on the row and column number position.

I'm not sure but probably your webElements array is empty that why You get Index out of bounds exception.

If you try to get value from entire WW_SALES row I suppose that find_elements should pint out parent div - class="ag-row ag-row-even ag-row-level-0"

It is only my supposition based on description and image attached.

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