Syed
Syed

Reputation: 187

How to exclude first and last row of a table when getting table size

I have a table with header(1row) and footer(1 row). i am getting the table size and expect value as 50 but it returns 52 as it count both header and footer row. Is there a way i can exclude the header and footer row with in my code and get the actual expected value. Here is my code to get the table size

public int getTableSize() throws InterruptedException{
    List<WebElement> columnElements = driver.findElements(By.cssSelector("table[class='table table-bordered table-hover ng-isolate-scope']>tbody>tr"));
  return  columnElements.size();
}

Upvotes: 2

Views: 504

Answers (2)

flowjow
flowjow

Reputation: 181

The most effective (or cheapest) way is to substract the number of elements to be removed, in your case the method should look like:

    public int getTableSize() throws InterruptedException{
        List<WebElement> columnElements = driver.findElements(By.cssSelector("table[class='table table-bordered table-hover ng-isolate-scope']>tbody>tr"));
        return  columnElements.size() - 2;
    }

I strongly recommend this as the primary solution because of the performance issues. For me, it's hard to imagine a situation where there's a need for a more complicated solution.

Upvotes: 0

koga73
koga73

Reputation: 1022

You can use :not in your selector to negate the first-child and last-child

table[class='table table-bordered table-hover ng-isolate-scope']>tbody>tr:not(:first-child):not(:last-child)

Upvotes: 2

Related Questions