Get the Dynamic table data from gui in selenium webDriver

Posted by Rabindra on Stack Overflow See other posts from Stack Overflow or by Rabindra
Published on 2013-08-01T06:04:56Z Indexed on 2013/10/25 21:55 UTC
Read the original article Hit count: 196

Filed under:

I am working on a web based Application that I am testing with Selenium. On one page the content is dynamically loaded in table. I want to get the Table data, i am geting a "org.openqa.selenium.NullPointerElementException" in this line.

WebElement table = log.driver.findElement(By.xpath(tableXpath));

I tried the following complete code.

public int selectfromtable(String tableXpath, String CompareValue, int columnnumber) throws Exception {
    WebElement table = log.driver.findElement(By.xpath(tableXpath));
    List<WebElement> rows = table.findElements(By.tagName("tr"));
    int flag = 0;
    for (WebElement row : rows) {
        List<WebElement> cells = row.findElements(By.tagName("td"));
        if (!cells.isEmpty() && cells.get(columnnumber).getText().equals(CompareValue)) {
            flag = 1;
            Thread.sleep(1000);
            break;
        } else {
            Thread.sleep(2000);
            flag = 0;
        }
    }
    return flag;
}

I am calling the above method like

String tableXpath = ".//*[@id='event_list']/form/div[1]/table/tbody/tr/td/div/table";
selectfromtable(tableXpath, eventType, 3);

my html page is like

<table width="100%">
    <tbody style="overflow: auto; background-color: #FFFFFF">
        <tr class="trOdd">
            <td width="2%" align="center">
            <td width="20%" align="center"> Account </td>
            <td width="20%" align="center"> Enter Collection </td>
            <td width="20%" align="center">
            <td width="20%" align="center"> 10 </td>
            <td width="20%" align="center"> 1 </td>
        </tr>
    </tbody>
    <tbody style="overflow: auto; background-color: #FFFFFF">
        <tr class="trEven">
            <td width="2%" align="center">
            <td width="20%" align="center"> Account </td>
            <td width="20%" align="center"> Resolved From Collection </td>
            <td width="20%" align="center">
            <td width="20%" align="center"> 10 </td>
            <td width="20%" align="center"> 1 </td>
        </tr>
    </tbody>
</table>

© Stack Overflow or respective owner

Related posts about selenium-webdriver