2011-02-04 66 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開始於

相關問題