1

I have a hide button in the application that hides duplicate data entries on web table. I have been trying to capture the number of hidden rows. See html and my approaches below. Every attempt I have tried ended up with 0. However, the result should be 2.

HTML CODE:

<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>
def getInvisibleTableRowCount()
{
    WebDriver driver = DriverFactory.getWebDriver()
    WebElement table = driver.findElement(By.xpath("//*[@id='DataTables_Table_0']/tbody"))
    List<WebElement> rows_table= table.findElements(By.cssSelector("[display=none]"));
    int rowSize = rows_table.size();
    return rowSize;
}

Here is my other attempt:

def getInvisibleTableRowCount()
{
    WebDriver driver = DriverFactory.getWebDriver()
    WebElement table = driver.findElement(By.xpath("//*[@id='DataTables_Table_0']/tbody"))
    List<WebElement> rows_table= table.findElements(By.tagName("tr[not(contains(@style,'display: none;'))]"));
    int rowSize = rows_table.size();
    return rowSize;
}

If I ran the xpath as //*[@id='DataTables_Table_0']/tbody/tr[not(contains(@style,'display: none;'))] , I can find the hidden rows on the browser.

I have also tried this:

def getInvisibleTableRowCount()
{
    WebDriver driver = DriverFactory.getWebDriver()
    WebElement table = driver.findElement(By.xpath("//*[@id='DataTables_Table_0']/tbody"))
    List<WebElement> rows_table= table.findElements(By.tagName("tr"));
    int rowSize = rows_table.size();
    for(WebElement row: rows_table)
    {
        if(row.isDisplayed()==false)
        {
            rowSize = rowSize -1; 
        }
    }
    return rowSize;
}

After @Hac's comment, I tried JQuery. I ran the jQuery on browser, it works with no problem. But I get a "NULL" value returned in my function. I double checked the jQuery string that prompts correct in the comment line.

@Keyword
def getTableRowCountAfterHiding()
{
    def jQuery='$'+'("#DataTables_Table_0 tbody tr:visible").length'
    WebUI.comment(jQuery);
    def visibleRowCounts = new utils.ExecuteJavaScript().executeJavaScript(jQuery);
    return visibleRowCounts;
}

I defined utils to run JS as it is in below:

public class ExecuteJavaScript {
    //This keyword is designed to execute JS.
    @Keyword
    def executeJavaScript(String javascript) {
        ((JavascriptExecutor) DriverFactory.getWebDriver()).executeScript(javascript)
    }
}
  • Selenium was designed to not interact with hidden elements to mimic a person interacting with the site. I suggest checking the total number of visible ones before and after instead. Or you could use javascript to force the interaction with hidden elements. https://stackoverflow.com/q/22110282/6205848 – HaC Aug 11 '20 at 23:33
  • 1
    Thanks for pointing that out. I have been struggling with this. I will try to execute JS. – BornToBeAGamer Aug 12 '20 at 00:34
  • 1
    I tried to execute JS, no luck so far. – BornToBeAGamer Aug 12 '20 at 15:13

1 Answers1

1

This worked:

def getTableRowCountAfterHiding()
{   
    WebDriver driver = DriverFactory.getWebDriver()
    List<WebElement> table = driver.findElements(By.xpath("//*[@id='DataTables_Table_0']/tbody/tr[not(contains(@style,'display: none;'))]"))
    int rowSize = table.size();
    return rowSize;
}