使用Selenium的Python API - 如何获取表中的行数?

时间:2011-02-04 21:31:39

标签: python selenium

如何使用Selenium的Python API获取HTML表格中的行数?

我有一个关键字和值的2列表,我想把它读成字典。类似的东西:

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

感谢, 杰米

2 个答案:

答案 0 :(得分:2)

来自司机:

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,])

答案 1 :(得分:0)

这是我目前的工作:

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

请注意,在is_element_present()方法中,行和列从1开始,而在get_table()行中,列从0开始