[Python] Exception has occurred: StaleElementReferenceException python selenium

Discussão em 'Python' iniciado por Stack, Fevereiro 18, 2025.

  1. Stack

    Stack Membro Participativo

    I am using python and selenium to read specific s on a page into an array. The page refreshes its data every 10 seconds or so automatically. I have the following code in a loop and it loops 5-200 times before getting the error.

    The error I get is " Exception has occurred: StaleElementReferenceException Message: stale element reference: stale element not found in the current frame" which is thrown on the line text_array.append(div.get_attribute('innerHTML'))

    I have tried a bunch of solutions I found in this group, including the try except section, but they have not prevented the error.

    Thanks for any help that can be given.

    text_array = []
    wait = WebDriverWait(driver, 10) # Wait for up to 10 seconds
    try:
    div_id = "tool-table-row"
    div_elements = wait.until(EC.presence_of_all_elements_located((By.ID, div_id)))
    except NoSuchElementException or StaleElementReferenceException:
    time.sleep(10)
    continue

    for div in div_elements:
    text_array.append(div.get_attribute('innerHTML'))

    Continue reading...

Compartilhe esta Página