Counting table rows (Web)

Last edited on

Here is an example of counting the rows (actually the number of TR tags) of a table:

def main():
    loadUrl("http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_tbody")
    n = "DOCUMENT.HTML1.BODY1.TABLE1"
    o = waitForObject(n)
    test.log("Number of rows: " + str(numberOfRows(o)))
    return

def numberOfRows(obj_or_name):
    table = waitForObject(obj_or_name)
    child = table.firstChild()
    row = None
    while not isNull(child):
        # If the table uses thead/tfoot/tbody around the rows
        if child.tagName == "TBODY":
            row = child.firstChild()
            break
        elif child.tagName == "TR":
            row = child
            break
        child = child.nextSibling()
    
    rowCount = 0
    if row is None:
        test.log("numberOfRows: Did not find any rows in the table")
    else:
        while not isNull(row):
            if (row.tagName == "TR"):
                rowCount = rowCount + 1
            row = row.nextSibling()

    return rowCount
test.py