I have a DOM structure "div> table id = 't1'> tbody> tr" (count of tr changes on resize) The javascript function changes the number of rows on resizing the browser. I want to count number of rows for the table.
I am using the following code
final String tablePath = "//../div/table[1]";
final int size = Browser.getElements(By.xpath(tablePath).tagName("tbody")).size();
System.out.println("row count=" + size);
How will I show the rowcount which is changing dynamically?
1 Answer 1
The size()
function returns the number of elements found with the getElements()
function, I expect it to return just one table element. The value of size equals 1.
I would get the HTML inside the table and count the occurrences of <tr>
or something unique per row, expecting you do not have nested tables with rows inside the initial table. The code for that would look something like this:
WebElement table = driver.findElement(By.id("t1"));
String tableContent = table.getAttribute("innerHTML");
int count = StringUtils.countMatches(tabelContent, "<tr>");
After you change the window size you will have to get the innerHTML
again and count the rows to see if it actually changed or not.