簡體   English   中英

Selenium Python StaleElementReferenceException:消息:元素不再有效。 我的表格未打印所有列值

[英]Selenium Python StaleElementReferenceException: Message: Element is no longer valid. My table is not printing all the column values

我正在打印表的列值。 有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)放進去,這樣我就能知道發生了什么。 僅打印一列。 通過使用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()

感謝一些幫助解決此問題的幫助。 謝謝,里亞茲

腳本執行后,更改元素的內部標識並且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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM