1

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:

browser = Selenium(...)
...
rows = ? (this is what I need)
for r = range(row):
    key = browser.get_table('tablename.' + r + '.0')
    value = browser.get_table('tablename.' + r + '.1')
    my_dict[key] = value

thanks, jamie

0

2 Answers 2

2

from the driver:

def get_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,])
2
  • Ok, but what xpath do I use? "//tablename" give a count of one, "//tabename/tr" give a count of 0.
    – Jamie Czuy
    Feb 4, 2011 at 22:30
  • //table[@id="yourtablesid"]/tr Feb 4, 2011 at 23:46
0

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] = value
    row = 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.