Skip to content

[py] Keep waiting when an element goes stale in three expected conditions - #18319

Open
Mohsintl wants to merge 1 commit into
SeleniumHQ:trunkfrom
Mohsintl:py-ec-stale-element
Open

Mohsintl wants to merge 1 commit into
SeleniumHQ:trunkfrom
Mohsintl:py-ec-stale-element

Conversation

@Mohsintl

Copy link
Copy Markdown
Contributor

🔗 Related Issues

None filed. The bug is small and the reproduction is below.

💥 What does this PR do?

element_to_be_clickable, element_located_to_be_selected and visibility_of_any_elements_located call is_displayed(), is_enabled() or is_selected() without catching StaleElementReferenceException.

If the page re-renders between finding the element and checking it (common with React, Vue or Angular), the exception escapes WebDriverWait.until. The wait only ignores NoSuchElementException by default, so it fails right away instead of polling again:

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "submit"))).click()
# StaleElementReferenceException raised from inside until(), although the
# re-rendered button is clickable a moment later

This PR makes these conditions return False on a stale element, so the wait retries.

🔧 Implementation Notes

This uses the same try/except StaleElementReferenceException: return False that visibility_of_element_located, visibility_of_all_elements_located and element_located_selection_state_to_be already use. It also matches Java, where ExpectedConditions.elementToBeClickable(By) and elementToBeClickable(WebElement) catch StaleElementReferenceException and keep polling.

When element_to_be_clickable is given a WebElement that goes stale, it now keeps waiting until the timeout, which is also what Java does.

The new unit tests in py/test/unit/selenium/webdriver/support/expected_conditions_tests.py use a mocked element whose checks raise StaleElementReferenceException. All 4 fail without the fix and pass with it.

🤖 AI assistance

  • No substantial AI assistance used
  • AI assisted (complete below)
    • Tool(s): Claude (Anthropic)
    • What was generated: it helped find the bug and wrote the fix and tests. I reviewed and checked all of it.
    • I reviewed all AI output and can explain the change

💡 Additional Considerations

None.

🔄 Types of changes

  • Bug fix (backwards compatible)

…ions

element_to_be_clickable, element_located_to_be_selected and
visibility_of_any_elements_located call is_displayed, is_enabled or
is_selected without catching StaleElementReferenceException. When the
page re-renders between finding the element and checking it, the
exception escapes WebDriverWait.until, which by default only ignores
NoSuchElementException, so the wait fails at once instead of polling
again.

Return False on a stale element, as visibility_of_element_located,
visibility_of_all_elements_located and the Java bindings'
ExpectedConditions.elementToBeClickable already do, so the wait retries.
@qodo-code-review

Copy link
Copy Markdown
Contributor

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@selenium-ci selenium-ci added C-py Python Bindings B-support Issue or PR related to support classes labels Nov 15, 2026
@selenium-ci

Copy link
Copy Markdown
Member

Thank you, @Mohsintl for this code suggestion.

The support packages contain example code that many users find helpful, but they do not necessarily represent
the best practices for using Selenium, and the Selenium team is not currently merging changes to them.

After reviewing the change, unless it is a critical fix or a feature that is needed for Selenium
to work, we will likely close the PR.

We actively encourage people to add the wrapper and helper code that makes sense for them to their own frameworks.
If you have any questions, please contact us

@cgoldberg cgoldberg left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected Conditions do handle stale elements inconsistently, but I'm not sure about changing behavior that could potentially break existing code relying on the current semantics.

You can already add any exception to WebDriverWait's ignored_exceptions if you want it ignored when using an Expected Condition.

@AutomatedTester any thoughts?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

B-support Issue or PR related to support classes C-py Python Bindings

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants