unknown
unknown

Reputation: 31

Selenium - How to count the number of rows in a table dynamically?

In my html page there's a table with 10 rows; those rows will display based on filter (dynamically). Let's say, for example, without any filters by default 10 row will be returned. after applying the filter, less than 10 rows will be returned, depending on the type of filter, so i want to get that dynamic count of table rows (after filter) using selenium web driver.

i tried with driver.findElements(By.xpath("abcd")).size() but this is giving default count 10; however, after applying the filter only 2 rows are appearing.

Please suggest how to get dynamic count (count=2 as appearing 2 rows in UI) .

Upvotes: 3

Views: 15505

Answers (2)

amogh
amogh

Reputation: 117

To find the total number of elements on dynamic webpage we need to use driver.findElements().size() method. But sometimes it's not useful at all.

First get the size of all element matching with the row count. Once we have it then you can use dynamic xpath ie replace row and column number run time to get the data.

{
    List<WebElement> rows_table = mytable.findElements(By.tagName("tr"));
    //To calculate no of rows In table.
    int rows_count = rows_table.size();

    //Loop will execute till the last row of table.
    for (int row=0; row<rows_count; row++){
    //To locate columns(cells) of that specific row.
    List<WebElement> Columns_row = rows_table.get(row).findElements(By.tagName("td"));
    //To calculate no of columns(cells) In that specific row.
    int columns_count = Columns_row.size();
    System.out.println("Number of cells In Row "+row+" are "+columns_count);

    //Loop will execute till the last cell of that specific row.
    for (int column=0; column<columns_count; column++){
    //To retrieve text from that specific cell.
        String celtext = Columns_row.get(column).getText();
        System.out.println("Cell Value Of row number "+row+" and column number "+column+" Is "+celtext);
    }
    System.out.println("--------------------------------------------------");
}

Upvotes: 2

Matt
Matt

Reputation: 74690

I'll prefix this with the fact that I'm not a Java dev but I use a lot of webdriver in other languages.

The best way to setup tests is to wait for the outcome of the action that your testing. Sometimes you can get away without doing waits, or timed waits, but then you go and run your tests on a slower grid box and everything falls in a heap.

Things like "div exists", "div has class", whatever your outcome may be. In your case, it sounds like you may not be able to test for a div to be rendered but you can probably use your size test as the outcome to wait for.

Selenium can use any ExpectedCondition or you can specify a function

WebDriverWait wait = new WebDriverWait(getDriver(), 5);
wait.until(new ExpectedCondition<Boolean>() {
    public Boolean apply(WebDriver driver) {
        int elementCount = driver.findElement(By.xpath("xxxx")).size();
        if (elementCount == 2)
            return true;
        else
            return false;
    }
});

Code from https://sqa.stackexchange.com/a/8701

Upvotes: 0

Related Questions