Skip to content Skip to sidebar Skip to footer

Using Selenium's Python Api - How Do I Get The Number Of Rows In A Table?

How do I get the number of rows in an HTML table using Selenium's Python API? I'd have a 2 column table of Keys and Values and I'd like to read it into a dictionary. Something like

Solution 1:

from the driver:

defget_xpath_count(self,xpath):
    """
    Returns the number of nodes that match the specified xpath, eg. "//table" would give
    the number of tables.

    'xpath' is the xpath expression to evaluate. do NOT wrap this expression in a 'count()' function; we will do that for you.
    """return self.get_number("getXpathCount", [xpath,])

Solution 2:

Here is my current work around:

row=0
while browser.is_element_present('//tablename//tr['+ str(row+1) +']/td[1]'):
    key = browser.get_table('tablename.'+ str(row) +'.0')
    value= browser.get_table('tablename.'+ str(row) +'.1')
    my_dict[key] =valuerow=row+1

Notice in the is_element_present() method rows and columns start at 1 versus in get_table() rows and columns start at 0

Post a Comment for "Using Selenium's Python Api - How Do I Get The Number Of Rows In A Table?"