How to avoid “StaleElementReferenceException” in Selenium?

前端 未结 16 2276
一向
一向 2020-11-22 12:12

I\'m implementing a lot of Selenium tests using Java. Sometimes, my tests fail due to a StaleElementReferenceException. Could you suggest some approaches to mak

16条回答
  •  迷失自我
    2020-11-22 12:58

    This works for me (100% working) using C#

    public Boolean RetryingFindClick(IWebElement webElement)
        {
            Boolean result = false;
            int attempts = 0;
            while (attempts < 2)
            {
                try
                {
                    webElement.Click();
                    result = true;
                    break;
                }
                catch (StaleElementReferenceException e)
                {
                    Logging.Text(e.Message);
                }
                attempts++;
            }
            return result;
        }
    

提交回复
热议问题