Selenium function to check for freshness before click to avoid stale element exception

276 Views Asked by At

I'm working on scraping data from a site which uses jquery and sometimes I get the Stale Element exception which I have fixed by introducing sleep between a try/except.

However, I wanted make it more robust and when going through the docs: https://selenium-python.readthedocs.io/waits.html#explicit-waits I found that there is a way to check if an element is stale with "staleness_of" method.

WebDriverWait(driver, 10).until(EC.staleness_of(driver.find_element_by_link_text("2020:")))

But it seems the above statement waits for the element to be stale which is the opposite of what I'm looking for i.e., is there a way to wait until an element is not stale and then proceed (to click it if possible)?

Test code: In which I'm trying to replace sleep()

from time import sleep

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.implicitly_wait(15)
driver.get('https://vahan.parivahan.gov.in/vahan4dashboard/')

driver.find_element_by_xpath('//*[@id="j_idt45"]').click() # Click Refresh

try:
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT,'2020:'))).click()
except Exception as e:
    print("Exception occoured!", e)
    sleep(5) # <============ Tying to replace this ============<
    try:
        WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT,'2020:'))).click()
    except:
        input("Year selection failed")
sleep(2) # <============ Tying to replace this ============<

text_string = driver.find_element_by_xpath('//*[@id="t3"]/div').text
1

There are 1 best solutions below

0
On

I a not a native python developer, however, I handled this in C# as :-

public static void StaleEnterTextExceptionHandler(this IWebDriver driver, By element, string value)
        {
            try
            {
                driver.EnterText(element, value);
            }
            catch (Exception e)
            {
                if (e.GetBaseException().Equals(typeof(StaleElementReferenceException)))
                {
                    StaleEnterTextExceptionHandler(driver, element, value);
                }
                else
                throw e;
            }
        }

Logic is simply recursively call the method unless the stale exception is not there.