【发布时间】:2018-05-23 09:44:51
【问题描述】:
我正在尝试从 selenium python 中的动态 web 表中检索数据,但控制台中的错误为 错误为“
values = self.driver.find_element_by_xpath(".//*[@id='table01']/tbody/tr["+row+"]/td["+col+"]").text
TypeError: 必须是 str,而不是 int
失败(错误=1)
class DynamicWebTable1(unittest.TestCase):
@classmethod
def setUpClass(cls):
chrome_driver_path = os.path.abspath('..') + "\\Drivers\\chromedriver.exe"
cls.driver=webdriver.Chrome(chrome_driver_path)
cls.driver.implicitly_wait(30)
cls.driver.maximize_window()
# navigate to the application home page
cls.driver.get("http://qavalidation.com/demo/")
def test_get_table_data(self):
time.sleep(10)
columns = len(self.driver.find_elements_by_xpath(".//*[@id='table01']/tbody/tr[1]/td"))
rows = len(self.driver.find_elements_by_xpath(".//*[@id='table01']/tbody/tr"))
print("rows - ",rows) # rows - 3
print("columns - ",columns) #columns - 4
for row in range(rows):
for col in range(columns):
values = self.driver.find_element_by_xpath(".//*[@id='table01']/tbody/tr["+row+"]/td["+col+"]").text
print(" Dynamic web table index {row} ,{col} value is {values} ".format(row, col, values))
@classmethod
def tearDownClass(cls):
# close the browser window
cls.driver.quit()
Github 示例代码https://github.com/venkywarriors619/selenium_with_python/blob/master/Python_basics/SeleniumWebDriver_Advanced/DynamicWebTable1.py
【问题讨论】:
-
试试
values = self.driver.find_element_by_xpath(".//*[@id='table01']/tbody/tr[%s]/td[%s]" % (row, col)).text
标签: python python-3.x selenium selenium-webdriver xpath