Selenium Stale Element Reference Errors (Seems Random)?

343 views Asked by At

I know there have been several questions asked regarding stale elements, but I can't seem to resolve these. My site is private so unfortunately can't share, but seems to always throw the error somewhere within the below for-loop. This loop is meant to get the text of each row in a table (number of rows varies). I've assigned WebDriverWait commands and have a very similar for-loop earlier in my code to do the same thing in another table on the website which works perfectly. I've also tried including the link click command and table, body, and tableText definition inside the loop to redefine at every iteration.

Once the code stops and the error message displays (stale element reference: element is not attached to the page document (Session info: chrome=89.0.4389.128)), if I manually run everything line-by-line, it all seems to work and correctly grabs the text.

Any ideas? Thanks!

link = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "*link address*")))
link.click()
table = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, "TableId")))
body = tableSig.find_element(By.CLASS_NAME, "*table body class*")
tableText = body.find_elements(By.TAG_NAME, "tr")
rows = len(tableText)
approvedSigs = [None]*rows

for i in range(1, rows+1):
    approvedSigs[i-1] = (tableText[i-1].text)
    approvedSigs[i-1] = approvedSigs[i-1].lstrip()
    approvedSigs[i-1] = approvedSigs[i-1][9:]
    approvedSigs[i-1] = approvedSigs[i-1].replace("\n","   ")
0

There are 0 answers