Search code examples
javaselenium-webdriverpage-factory

Always gives me row count of 0 when I try to get through WebTable in selenium


package LoginPagePLot;

import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.WebDriverWait;

public class SearchRecord {
    public WebDriver driver;

    public SearchRecord(WebDriver driver1) {
        this.driver = driver1;
        WebDriverWait wait = new WebDriverWait(driver, 30);    
    }

    public void clickonuser() {
        WebElement tbody = driver.findElement(By.tagName("tbody"));
        // Now get all the TR elements from the table 
        List<WebElement> allRows = tbody.findElements(By.cssSelector("tbody>tr")); 
        System.out.println(allRows.size());

        // And iterate over them, getting the cells 
        for (WebElement row : allRows) { 
            List<WebElement> cells = row.findElements(By.tagName("td")); 

            // Print the contents of each cell
            for (WebElement cell : cells) { 
                System.out.println(cell.getText());
            }
        }
    }       
}

Test case class

package LoginTestCasePlot;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import LoginPagePLot.AddIndividual;
import LoginPagePLot.SearchRecord;


public class VerifySearchRecord {
    public WebDriver driver;
    VerifyloginPF verifylogin=new VerifyloginPF();
    VerifyHomepage verifyhome= new VerifyHomepage();
    VerifyAddIndividual verifyadd=new VerifyAddIndividual();

    @BeforeTest
    public void verifydatainputed() {
        System.out.println("Record added");
        verifyadd.verifydashboardisplayed();
        driver=verifyadd.Adduserdata123();
        System.out.println("Record added in list");
    }
    @Test
    public void viewindividual() {
        SearchRecord srch=PageFactory.initElements(driver, SearchRecord.class);
        srch.clickonuser();
    }    
}

HTML:

<table id="myTable" class="table table-striped dataTable no-footer" role="grid" aria-describedby="myTable_info" style="width: 1061px;">
        <thead>
            <tr role="row"><th class="single-line sorting_asc" tabindex="0" aria-controls="myTable" rowspan="1" colspan="1" style="width: 58px;" aria-sort="ascending" aria-label="
                    Family ID
                : activate to sort column descending">
                    Family ID
                </th><th class="single-line sorting" tabindex="0" aria-controls="myTable" rowspan="1" colspan="1" style="width: 104px;" aria-label="
                    Person Name
                : activate to sort column ascending">
                    Person Name
                </th><th class="sorting" tabindex="0" aria-controls="myTable" rowspan="1" colspan="1" style="width: 94px;" aria-label="
                    CNIC
                : activate to sort column ascending">
                    CNIC
                </th><th class="sorting" tabindex="0" aria-controls="myTable" rowspan="1" colspan="1" style="width: 121px;" aria-label="Registered At: activate to sort column ascending">Registered At</th><th class="sorting" tabindex="0" aria-controls="myTable" rowspan="1" colspan="1" style="width: 35px;" aria-label="City: activate to sort column ascending">City</th><th class="sorting_disabled" rowspan="1" colspan="1" style="width: 57px;" aria-label="
                    Personas
                ">
                    Personas
                </th><th class="sorting_disabled" rowspan="1" colspan="1" style="width: 97px;" aria-label="Qualifications">Qualifications</th><th class="sorting" tabindex="0" aria-controls="myTable" rowspan="1" colspan="1" style="width: 40px;" aria-label="Status: activate to sort column ascending">Status</th><th class="text-center sorting_disabled" rowspan="1" colspan="1" style="width: 59px;" aria-label="Action(s)">Action(s)</th></tr>
        </thead>

000010 abbas18720-9298862-8

1/22/2019Islambad 000010 abbas18720-9198862-8

1/22/2019Islambad 000010 abbas18721-9212843-8

1/22/2019Islambad 000010 abbas18721-9223130-8

1/22/2019Islambad 000010 abbas18721-9223140-8

1/22/2019Islambad 000010 abbas18721-9298862-8

1/22/2019Islambad 000010 abbas18721-9223110-8

1/22/2019Islambad 000010 abbas18721-9223813-8

1/22/2019Islambad 000010 abbas18721-9298843-8

1/22/2019Islambad 000010 abbas18721-9223810-8

1/22/2019Islambad

It always returns Row size=0 although there are more than 10 rows in table But I am unable to get the rowsize due to which I can not execute my test. I have tried it through xpath, css selector and tagname but returns nothing in rows result when I print the row.size();

Please help me to resolve this problem.


Solution

  • The problem was because table was loaded dynamically so he had to wait until the whole table will be available. Thread.sleep(2000) solve this problem but it is not good solution. I suggest to use WebDriverWait and .until() method which will get elements when they will be loaded.

    List<WebElement>trList = wait.until(ExpectedConditions.visibilityOfNestedElementsLocatedBy(tbody, By.tagName("tr")));