[Python] Selenium element raise StaleElementReferenceException even with explicit wait

Discussão em 'Python' iniciado por Stack, Março 11, 2025.

  1. Stack

    Stack Membro Participativo

    When I call the below statement:

    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, tag))).click()


    I was periodically getting a StaleElementReferenceException, which I thought shouldn't happen considering the explicit wait I've used.

    I had to use the below code fixed the issue but I don't understand why the above can ever raise StaleElementReferenceException in the 1st place.

    def click_element_with_retry(driver, tag, seconds):
    # for some reason the element_to_be_clickable by itself is not working, and periodically retruns a StaleElementReferenceException
    end_time = datetime.now() + timedelta(seconds=seconds)
    while datetime.now() < end_time:
    try:
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, tag))).click()
    break
    except TimeoutException:
    raise_vba_error_message_and_quit(driver, f"Mars Web element could not be found after timeout", "Timeout Error")
    except StaleElementReferenceException:
    continue


    Any suggested reading or thoughts why this might be happening? I'm using a edge driver if that matters

    Continue reading...

Compartilhe esta Página