Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
in DevOps and Agile by (19.7k points)

I'm implementing a lot of Selenium tests using Java. Sometimes, my tests fail due to a StaleElementReferenceException. Could you suggest some approaches to making the tests more stable?

1 Answer

0 votes
by (62.9k points)

This can happen if a DOM operation happening on the page is temporarily causing the element to be inaccessible. To allow for those cases, you can try to access the element several times in a loop before finally throwing an exception.

 public boolean retryingFindClick(By by) {

    boolean result = false;

    int attempts = 0;

    while(attempts < 2) {

        try {

            driver.findElement(by).click();

            result = true;

            break;

        } catch(StaleElementException e) {

        }

        attempts++;

    }

    return result;

}

If you are interested to learn Selenium on a much deeper level and want to become a professional in the testing domain, check out Intellipaat’s Selenium training courses

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...