Tag Archives: StaleElementReferenceException

Selenium: How to Solve Staleelementreferenceexception error (Three Methods)

In the process of automatic testing, the code has no errors, and the following errors can be encountered many times during running:
selenium. Common. Exceptions. Elementnotinteractableexception: Message: element not interactable

At present, I think of the following three solutions:
method 1:
using time. Sleep() can reduce the probability of this exception, but it is not 100% effective and wastes time.

Method 2:
use driver. Refresh() to refresh the page and then reposition this element.

Method 3:
in many cases during the test process, there will be deviations in the test results after refreshing the page. In this case, we can only use the method of capturing exceptions to avoid the current exceptions. That is, reposition the element when an exception occurs.

1    # time.sleep(2)
2    # driver.refresh()
3    try:
4        driver.find_element_by_id("details-button").click()
5        driver.find_element_by_id("proceed-link").click()
6    except:
7        driver.find_element_by_id("details-button").click()
8        driver.find_element_by_id("proceed-link").click()