titli0216
titli0216

Reputation: 37

How to read cell values in a table using selenium webdriver

[Below is the html code. Actually I am unable to understand how to actually explain the condition the webtable has two table ids, tbl and tbr and the table has a total of 4 rows.The first column in under the table id=tbl and the remaining columns Rev and Qty and others under the table id=tbr.I am trying to check the col values exists or not considering each row for example col1 under table id=tbl is a Part Name and col2 and col3 under table id=tbr has Part Revision and Part Quantity. I want to check if the Part exists with the Revision and Quantity specified.Is my question clear now?

<form autocomplete="off" onsubmit="return false;" name="tvcTableForm">
    <input type="hidden" value="tvc-0000000000000002" name="object">
    <input type="hidden" value="35004.36845.30464.63080" name="parentOID">
    <div id="th">
    <div id="tb">
    <div id="tblc" style="top: 32px; height: 112px;">
    <table id="tbl" class="contentTable" cellspacing="0" cellpadding="0" style="border: 0px none; width: 199px; height: 144px; table-layout: fixed;">
    <tbody>
    </table>
    </div>
    <div id="tbrc" style="top: 32px; left: 199px; width: 1106px; height: 112px; overflow: auto;">
    <table id="tbr" class="contentTable" cellspacing="0" cellpadding="0" style="border: 0px none; width: 1089px; height: 144px; table-layout: fixed;">
    <tbody>
    <tr id="r573378134017" class="even" oncontextmenu="return ctxm(event)" style="height: 36px;">

Upvotes: 1

Views: 13447

Answers (2)

JohnP2
JohnP2

Reputation: 2167

Here is another way: use javascript.

String cellValue = js.executeScript("return arguments[0].children[0].children[arguments[1]].children[arguments[2]].innerText", tableElement, rowIndex, columnIndex));

This is based on the following javascript code where r = rowIndex and c = columnIndex

document.querySelector("#table").children[0].children[r].children[c].innerText

Upvotes: 0

frianH
frianH

Reputation: 7563

This is the way to get the value of each cell of a web table using selenium with java language.

1. Table initialize

In your case, the table has the id tbr, the way to initialize it :

WebElement tbl = driver.findElement(By.id("tbr"));

2. Row initialize

The name tag for the web table row in general is tr, the way to initialize it :

List<WebElement> rows = tbl.findElements(By.tagName("tr"));

3. Column initialize

The name tag for the web table column in general are th or td, the way to initialize it :

th tag

List<WebElement> cols = rows.get(rowIndex).findElements(By.tagName("th"));

td tag

List<WebElement> cols = rows.get(rowIndex).findElements(By.tagName("td"));

So you can get particular cell values by :

String cell = cols.get(indexCol).getText();

The bellow code is function to get all value cell table (without getting header th value)

WebElement tbl = driver.findElement(By.id("tbr"));

//check all row, identification with 'tr' tag
List<WebElement> rows = tbl.findElements(By.tagName("tr"));

//row iteration
for(int i=0; i<rows.size(); i++) {
    //check column each in row, identification with 'td' tag
    List<WebElement> cols = rows.get(i).findElements(By.tagName("td"));

    //column iteration
    for(int j=0; j<cols.size(); j++) {
        System.out.println(cols.get(j).getText());
    }

    //This is to get the cell value you want
    ////get col no 2
    //System.out.println(cols.get(1).getText());
    ////get col no 8
    //System.out.println(cols.get(7).getText());
}

Upvotes: 1

Related Questions