2011-02-04 35 views
1

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

我会有一个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

从驱动程序:

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

好的,但我使用什么xpath? “// tablename”给出一个计数,“// tabename/tr”计数为0. –

+0

// table [@ id =“yourtablesid”]/tr –

0

这是我目前的解决方法:在is_element_present

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 

通知()方法行和列从1开始,而在get_table()行和列从0开始于

相关问题