Anna
Anna

Reputation: 137

size of a table, size() returns 0

Hi I'm trying to get the number of rows of a table in this way:

int rows = findElements(By.xpath("//div[@class='communications-table ohim-table dataTable']/tbody/tr")).size();

But it returns 0 rows. I do have rows in my table, any idea? Thanks.

HTML:

    <div class="box-content detailsBoxBody">
      <div id="trademarksCommunicationsTable">
       <div id="DataTables_Table_5_wrapper" class="dataTables_wrapper" role="grid">
        <div class="datatables-top full-width">
         <div id="DataTables_Table_5_processing" class="dataTables_processing" style="visibility: hidden;">Processing...</div>
           <table id="DataTables_Table_5" class="communications-table ohim-table dataTable" aria-describedby="DataTables_Table_5_info">
             <thead>
              <tbody role="alert" aria-live="polite" aria-relevant="all">
                <tr class="odd">
                <tr class="even">
                <tr class="odd">
                <tr class="even">
                <tr class="odd">
                <tr class="even">
                <tr class="odd">
                <tr class="even">
                <tr class="odd">
                <tr class="even">
              </tbody>
            </table>
          .....

Upvotes: 0

Views: 281

Answers (2)

Saifur
Saifur

Reputation: 16201

I think the wait is an issue too. Try using explicit wait with that. And, notice I am using css

By byCss = By.cssSelector(".communications-table.ohim-table.dataTable tr");

List<WebElements> elements = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfAllElementsLocatedBy(byCss));

elements.size();

Upvotes: 2

SiKing
SiKing

Reputation: 10329

There is no chance that your XPath:

By.xpath("//div[@class='communications-table ohim-table dataTable']/tbody/tr")

is ever going to find a table!

Try:

By.xpath("//table[@id='DataTables_Table_5']//tr")

Upvotes: 1

Related Questions