Selenium Python列中的表行未存储在my list变量中
我有一个包含一些列和行的表的页面。我试图将第1列中每行的数据存储到一个列表变量中。当我浏览我的代码时,列表变量中没有存储任何内容。 我遍历表的循环很好,因为我可以为每行打印第1列中的值。 我已经在类级别声明了List变量 我的代码片段是:Selenium Python列中的表行未存储在my list变量中,python,python-2.7,selenium,selenium-webdriver,Python,Python 2.7,Selenium,Selenium Webdriver,我有一个包含一些列和行的表的页面。我试图将第1列中每行的数据存储到一个列表变量中。当我浏览我的代码时,列表变量中没有存储任何内容。 我遍历表的循环很好,因为我可以为每行打印第1列中的值。 我已经在类级别声明了List变量 我的代码片段是: def get_table_column_1_values_into_a_list(self): try: WebDriverWait(self.driver, 20).until(EC.presence_of_element_loca
def get_table_column_1_values_into_a_list(self):
try:
WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((By.ID, 'data_configuration_data_previews_ct_fields_body')))
table_id = WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((By.ID, 'data_configuration_data_previews_ct_fields_body')))
rows = table_id.find_elements(By.TAG_NAME, "tr")
for row in rows:
# Get the columns
col_name = row.find_elements(By.TAG_NAME, "td")[1] # This is the Configuration Name column
col_type = row.find_elements(By.TAG_NAME, "td")[3] # This is the Type column
col_rows = row.find_elements(By.TAG_NAME, "td")[4] # This is the Rows column
print "col_name.text = "
print col_name.text
print col_type.text
print col_rows.text
# store the value of col 1 into a list
DataPreviewsViewPage.col1_list_before_sort[row] = col_name.text
print DataPreviewsViewPage.col1_list_before_sort[row]
# return True
return DataPreviewsViewPage.col1_list_before_sort
#return False
except NoSuchElementException, e:
print e
return False
我的列表变量声明是:
class DataPreviewsPage(BasePage):
col1_list_before_sort = []
在For循环中,如何将col\u name=row.find\u元素(By.TAG\u name,“td”)[1]
中的值存储到项目列表中
谢谢,
Riaz我认为您可以从该方法返回列名列表:
def get_table_column_1_values_into_a_list(self):
try:
WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((By.ID, 'data_configuration_data_previews_ct_fields_body')))
table_id = WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((By.ID, 'data_configuration_data_previews_ct_fields_body')))
rows = table_id.find_elements(By.TAG_NAME, "tr")
return [row.find_element(By.TAG_NAME, "td")[1].text for row in rows]
except NoSuchElementException, e:
print e
return False
不确定代码是如何运行的。
row
不是一个webElement和DataPreviewViewPage.col1\u list\u before\u sort[row]
不能工作,如果它是一个列表?我可以将列名列表存储到列表或数组中吗?然后我想按字母顺序对列表进行排序。我想使用列表数据类型的排序函数。例如,col1\u values\u list.sort()@RiazLadhani我发布的方法已经返回了一个列表。是的,它已经返回了一个列表。我用PyCharm调试程序浏览了代码。它的每一行都有col1中的名称。非常感谢你的帮助。