[Python] Stale element reference: stale element not found on Python Selenium

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

  1. Stack

    Stack Membro Participativo

    I have the following (part of a) the source code of a web page :

    <div role="main">
    <span id="maincontent"/>
    <div class="attempt-info-container">
    <h3 class="attempt-question-number">Question 22 / 100</h3>
    <div class="attempt-progress progress">
    <div class="progress-bar" role="progressbar" style="width: 22%" aria-valuenow="22" aria-valuemin="0" aria-valuemax="100"/>
    </div>
    <p class="question-info">
    <label>Réf. question : </label>
    <span class="question-ref">BU#02839</span>
    </p>
    <p class="category-info">
    <label>Catégorie : </label>
    <span class="category-name">1.8 - Some question category</span>
    </p>
    </div>
    <div id="question-page-container">
    <form id="responseform" method="post" action="https://somepage/attempt.php?attemptid=4710638" enctype="multipart/form-data">
    <div>
    <input type="hidden" name="sesskey" value="UgU8k0GICH">
    <input type="hidden" name="slots" value="22"/>
    <div id="question-6986754-22" class="que multichoice immediatefeedback notyetanswered">
    <div class="info">
    <div class="state">Incomplete</div>
    <div class="grade">Noted on 1000,00</div>
    <div class="questionflag">
    <img src="https://someimagesource/1742007605/i/unflagged" alt="Non marked" class="questionflagimage"/>
    <span>Marquer la question</span>
    </div>
    </div>
    <div class="content">
    <div class="formulation clearfix">
    <h4 class="accesshide">Text of the question</h4>
    <input type="hidden" name="q6986754:22_:sequencecheck" value="1"/>
    <div class="qtext">
    <p>2839 - some blah blah:</p>
    </div>
    <div class="ablock">
    <div class="prompt">Please select an answer.</div>
    <div class="answer">
    <div class="r0">
    <input type="radio" name="q6986754:22_answer" value="0" id="q6986754:22_answer0"/>
    <label for="q6986754:22_answer0" class="ml-1">
    <span


    and I try to get the text 1.8 - Some question category from

    <span class="category-name">1.8 - Some question category</span>


    as follows :

    ignored_exceptions=(EC.NoSuchElementException,EC.StaleElementReferenceException,)
    category_element = WebDriverWait(driver, 150, ignored_exceptions=ignored_exceptions).until(EC.element_to_be_clickable((By.CLASS_NAME, "category-name")))
    category = category_element.text


    and I sometimes (after my chrome driver has browsed a certain number of pages of the same type) get a

    Exception has occurred: StaleElementReferenceException
    Message: stale element reference: stale element not found


    on the second line. I tried with visibility_of_element_located instead of element_to_be_clickable and then the exception happens even sooner. I alse tried the second inside a for loop with trials inside it, like "do it until category_element is true", still have the same exception.

    (I basically everything proposed in StaleElementReferenceException on Python Selenium without success.)

    What I remarked, if it can be of any help but I don't think it could, is that the same code with same python, packages, chrome and chrome driver on different computer at home and at works tends to yield the exception sooner than later, but the exception always finishes to happen, while would I operate the site myself manually, I would never have the exception.

    Continue reading...

Compartilhe esta Página