Returning the visible table rows with JS

I executed the jQuery in the browser and it returns 5. However, in my function it returns null.
Here is the html:

> <tbody>
> <tr role = "row" class="odd">...<tr/>
> <tr role = "row" class="even">...<tr/>
> <tr role = "row" class="odd">...<tr/>
> <tr role = "row" class="even">...<tr/>
> <tr role = "row" class="odd">...<tr/>
> <tr role = "row" class="odd duplicate" style="display: none;" >...<tr/>
> <tr role = "row" class="even duplicate" style="display: none;" >...<tr/>
> </tbody>

Here is my JS:

def getTableRowCountAfterHiding()
def jQuery=’$’+’("#DataTables_Table_0 tbody tr:visible").length’
def visibleRowCounts = WebUI.executeJavaScript(jQuery, null);
return visibleRowCounts;

On the face of it, your jQuery/JS code appears to work (obviously, I couldn’t use your IDs)

That code is running against THIS forum page.

This is how I might write your code:

String js = '\$("#DataTables_Table_0 tbody tr:visible").length;'
int len = (int) WebUI.executeJavaScript(js, null)
return len

The other thing you might try is writing the comparison test inside the JavaScript and return a boolean if the comparison succeeds. That is how I test row counts.

Thank you very much. I will give it a try. I also come with a different solution. I will share in case if anyone runs into this later. This will return the visible ones after hiding some table rows.

WebDriver driver = DriverFactory.getWebDriver()
List table = driver.findElements(By.xpath("//*[@id=‘DataTables_Table_0’]/tbody/tr[not(contains(@style,‘display: none;’))]"))
int rowSize = table.size();
return rowSize;