我正在打印表格的列值。共有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
我首先得到列表索引超出范围错误。然后我把打印len(行),以便我可以找出发生了什么。只打印了一列。 通过使用print len(行)我发现它只有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()
有些人帮助解决了这个问题。 谢谢, 里亚兹
答案 0 :(得分:0)
脚本执行后可能会更改元素的内部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