Python Selenium函数在单击之前检查新鲜度,以避免过时元素异常

Python Selenium函数在单击之前检查新鲜度,以避免过时元素异常,python,selenium,selenium-webdriver,exception,staleelementreferenceexception,Python,Selenium,Selenium Webdriver,Exception,Staleelementreferenceexception,我正在从一个使用jquery的站点上抓取数据,有时我会得到过时的元素异常,我通过在try/except之间引入sleep来修复这个异常 但是,我想让它更健壮,在浏览文档时:我发现有一种方法可以使用“staleness_of”方法检查元素是否过时 WebDriverWait(driver,10).until(EC.staleness_of(driver.find_element_by_link_text)(“2020:)) 但是上面的语句似乎在等待元素过时,这与我正在寻找的相反,也就是说,有没有

我正在从一个使用jquery的站点上抓取数据,有时我会得到过时的元素异常,我通过在try/except之间引入sleep来修复这个异常

但是,我想让它更健壮,在浏览文档时:我发现有一种方法可以使用“staleness_of”方法检查元素是否过时

WebDriverWait(driver,10).until(EC.staleness_of(driver.find_element_by_link_text)(“2020:))
但是上面的语句似乎在等待元素过时,这与我正在寻找的相反,也就是说,有没有一种方法可以等到元素没有过时,然后继续(如果可能的话单击它)

测试代码:其中我试图替换sleep()

从时间导入睡眠
从selenium导入webdriver
从selenium.webdriver.common.by导入
从selenium.webdriver.support.ui导入WebDriverWait
从selenium.webdriver.support将预期的_条件导入为EC
driver=webdriver.Chrome()
驱动程序。隐式等待(15)
司机,上车https://vahan.parivahan.gov.in/vahan4dashboard/')
驱动程序。通过xpath('/*[@id=“j_idt45”]”查找元素。单击()
尝试:
WebDriverWait(驱动程序,10)。直到(EC.element可点击((By.LINK文本,'2020:'))。点击()
例外情况除外,如e:
打印(“异常发生!”,e)

sleep(5)#我不是本地python开发人员,但是,我在C中处理这个问题的方式是:-

逻辑只是递归地调用方法,除非不存在过时的异常

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;
            }
        }