I have a page with a HTML table with 16 rows and 5 columns. I have a method to loop through the table and print out the cell values. I get the following error:
raise exception_class(message, screen, stacktrace)
StaleElementReferenceException: Message: Element is no longer valid
The error happens on this line:
col_name = row.find_elements(By.TAG_NAME, "td")[1] # This is the Name column
My method code is:
def get_variables_col_values(self):
try:
table_id = self.driver.find_element(By.ID, 'data_configuration_variables_ct_fields_body1')
#time.sleep(10)
rows = table_id.find_elements(By.TAG_NAME, "tr")
print "Rows length"
print len(rows)
for row in rows:
# Get the columns
print "cols length"
print len(row.find_elements(By.TAG_NAME, "td"))
col_name = row.find_elements(By.TAG_NAME, "td")[1] # This is the Name column print "col_name.text = "
print col_name.text
except NoSuchElementException, e:
return False
Am i getting the element is no longer valid because the dom has updated, changed? The table has not completed in loading? How can i solve this please? Do i need to wait for the page to be fully loaded, completed?
Should i use the following WebdriverWait code to wait for page load completed?
WebDriverWait(self.driver, 10).until(lambda d: d.execute_script('return document.readyState') == 'complete')
Where about in my code should i put this line if this is required?
I ran my code again, the 2nd time it worked. The output was:
Rows length
16
cols length
6
col_name.text =
Name
cols length
6
col_name.text =
Address
cols length
6
col_name.text =
DOB
...
So I need to make my code better so it works every time i run my test case. What is the best solution?
Thanks, Riaz