【问题标题】:Selenium Python StaleElementReferenceException: Message: Element is no longer valid. My table is not printing all the column valuesSelenium Python StaleElementReferenceException:消息:元素不再有效。我的表没有打印所有列值
【发布时间】:2015-08-20 13:21:42
【问题描述】:

我正在打印表格的列值。有 5 列。我打印出 Name、Dataset 和 Datamap col 值。

col_name = row.find_elements(By.TAG_NAME, "td")[0] # This is the Name column
col_dataset = row.find_elements(By.TAG_NAME, "td")[1] # This is the Dataset column
col_datamap = row.find_elements(By.TAG_NAME, "td")[2] # This is the Datamap
print col_name.text
print col_dataset.text
print col_datamap.text

一开始我遇到了列表索引超出范围错误。然后我把 print len(rows) 这样我就可以知道发生了什么。只打印了 1 列。 通过使用 print len(rows) 我发现它只有 1 列 我认为其他 4 列尚未绘制和渲染。页面未完成。

第二次调用我的方法(来自 TestCase 2),表中有第二行。它打印正确的列长度 5 并打印所有值。

开发人员说等待页面完成或等到所有元素都加载完毕。 我尝试在调用以下代码行之前放置 time.sleep(10):

time.sleep(10)
rows = table_id.find_elements(By.TAG_NAME, "tr")

在for循环里面我也试过WebdriverWait,下面这行代码:

WebDriverWait(self.driver, 10).until(lambda d: d.execute_script('return document.readyState') == 'complete')

我收到以下错误元素不再有效:

raise exception_class(message, screen, stacktrace)
StaleElementReferenceException: Message: Element is no longer valid

这是我调用以从表中打印列值的方法:

    def get_feeds_col_values(self):
    try:
        table_id = self.driver.find_element(By.ID, 'data_configuration_feeds_ct_fields_body0')
        time.sleep(10)
        rows = table_id.find_elements(By.TAG_NAME, "tr")
        #wait = WebDriverWait(self.driver, 10)
        #element = wait.until(self.driver.execute_script("return document.readyState;") == "complete")
        WebDriverWait(self.driver, 10).until(lambda d: d.execute_script('return document.readyState') == 'complete')
        print "Rows length"
        print len(rows)
        for row in rows:
            #time.sleep(10)
            WebDriverWait(self.driver, 10).until(lambda d: d.execute_script('return document.readyState') == 'complete')
            print "cols length"
            print len(row.find_elements(By.TAG_NAME, "td"))
            col_name = row.find_elements(By.TAG_NAME, "td")[0] # This is the Name column
            col_dataset = row.find_elements(By.TAG_NAME, "td")[1] # This is the Dataset column
            col_datamap = row.find_elements(By.TAG_NAME, "td")[2] # This is the Datamap
            print col_name.text
            print col_dataset.text
            print col_datamap.text
    except NoSuchElementException, e:
       print "NoSuchElementException" + e

调用方法的TestCase 1:

def test_add_crm_feeds(self):
    print "*** Test add crm feeds ***"
    data_dashboard_page = self.login_page.userLogin_valid(Globals.login_username, Globals.login_password)
    md = main_dashboard.MainDashboardPage(self.driver)
    md.select_project_from_drop_down()
    data_configuration_page = data_dashboard_page.click_data_configuration2() # Click Data Configuration from the Project Navigator
    assert data_configuration_page.is_Data_Configuration_pageDisplayed(), "Data Configuration Page not displayed"
    if data_configuration_page.is_Data_Configuration_pageDisplayed():
       print "ERROR - Data Configuration page is not displayed"
    projectNavigator = project_navigator.ProjectNavigatorPage(self.driver)
    feedsPage = projectNavigator.select_projectNavigator_item("Feeds")
    self.assertTrue(feedsPage.is_feeds_pageDisplayed(), "Feeds page not showm. We have not landed on the correct Feeds page.  See error log for details")
    if not feedsPage.is_feeds_pageDisplayed():
        print "ERROR - Feeds page is not displayed"
    feedsPage.click_add_feeds()
    feedsPage.enter_feed_name("crm") # Enter crm for the feeds name
    feedsPage.select_datamap_from_dropdown()
    feedsPage.select_dataset_from_dropdown("CRM")
    feedsPage.click_fields_tab()
    feedsPage.select_preview_to_import_fields_drop_down(Globals.datapreview_crm_name)
    if not feedsPage.is_crmid_checkbox_selected():
        print "CRMID checkbox is not checked by default - Going to click it"
        feedsPage.click_crmid_checkbox()
    feedsPage_saved = feedsPage.click_save2()
    feedsPage_saved.get_feeds_col_values()

调用方法的TestCase 2:

def test_add_escr_feeds(self):
    print "*** Test add escr feeds ***"
    projectNavigator = project_navigator.ProjectNavigatorPage(self.driver)
    feedsPage = projectNavigator.select_projectNavigator_item("Feeds")
    feedsPage.click_add_feeds()
    feedsPage.enter_feed_name("escr") # Enter escr for the feeds name
    feedsPage.select_datamap_from_dropdown()
    feedsPage.select_dataset_from_dropdown("ESCR")
    feedsPage.click_fields_tab()
    feedsPage.select_preview_to_import_fields_drop_down(Globals.datapreview_escr_name)
    if not feedsPage.is_crmid_checkbox_selected():
        print "ESCRID checkbox is not checked by default - Going to click it"
        feedsPage.click_crmid_checkbox()
    feedsPage_saved = feedsPage.click_save2()
    feedsPage_saved.get_feeds_col_values()

感谢一些帮助来解决这个问题。 谢谢, 里亚兹

【问题讨论】:

    标签: python python-2.7 selenium selenium-webdriver


    【解决方案1】:

    可能在脚本执行后元素的内部 id 发生变化,webdriver 无法在新的 DOM 树中找到旧行。检查这个假设:

    table_id = self.driver.find_element(By.ID, 'data_configuration_feeds_ct_fields_body0')    
    rows = table_id.find_elements(By.TAG_NAME, "tr")
    WebDriverWait(self.driver, 10).until(lambda d: d.execute_script('return document.readyState') == 'complete')
    print table_id.id, [row.id for row in rows] # initial ids of elements
    for row in rows:
        WebDriverWait(self.driver, 10).until(lambda d: d.execute_script('return document.readyState') == 'complete')
        table_id = self.driver.find_element(By.ID, 'data_configuration_feeds_ct_fields_body0')        
        rows = table_id.find_elements(By.TAG_NAME, "tr")
        print table_id.id, [row.id for row in rows] # after script execution
    

    【讨论】:

    • 表中的表 ID 正在更改,因此在 DOM 树中找不到旧元素。开发人员现在已经删除了这个号码,我现在已经开始工作了。感谢您的假设,它帮助我确定了正在发生的事情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 2020-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多