定位表格行和单元格
# -*- coding:utf-8 -*- from selenium import webdriver from selenium.webdriver.common.by import By import unittest class TableTest(unittest.TestCase): def setUp(self): self.driver = webdriver.Firefox() def test_table(self): driver = self.driver driver.get('http://www.w3school.com.cn/html/html_tables.asp') #得到所有的 tr 集合 rows = driver.find_elements_by_css_selector('.dataintable>tbody>tr') print 'Table 行数: ', len(rows) for tr in rows: tds = tr.find_elements_by_tag_name('td') #得到当前 tr 里的 td 集合 if tds: for td in tds: print td.text #得到 td 中的文本 def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main()