python - Selenium Python 列中的表行未存储在我的列表变量中

标签 python python-2.7 selenium selenium-webdriver

我有一个页面,其中包含一些列和行的表格。我试图将每行第一列的数据存储到列表变量中。当我浏览代码时,列表变量中没有存储任何内容。 我迭代表的循环很好,因为我可以打印出每行第一列的值。 我已在类级别声明了 List 变量。

我的代码片段是:

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_name = row.find_elements(By.TAG_NAME, "td")[1] 中的值存储到项目列表中?

谢谢, 里亚兹

最佳答案

我认为您可以从方法中返回列名称列表:

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

关于python - Selenium Python 列中的表行未存储在我的列表变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35384450/

相关文章:

python - 列出Python包中同名的方法

python - 将字节附加到字符串?

java - 无法在 Java 中使用 phantomJS 处理警报

java - anchor 标记不可点击

python - 打开文件的最pythonic方式是什么?

python - 值错误: Sample larger than population or is negative

Python Twitter - 获取推文 URL

python - 在数据框/二维数组中查找元组的行平均值

python-2.7 - 属性错误 : 'module' object has no attribute 'RLock' `

java - boolean 检查后单击单选按钮