0

I have some code where an expected exception is not being caught. I'm trying to find an element which most of the time will be stale. So I loop 60 times attempting to get the element before I get a element not found exception. It doesn't print out that it caught the exception even though I still get a stale element exception.

public static WebElement DropDown(WebDriver driver) throws InterruptedException
{
    WebElement element = null;
    for (int i = 0; i < 60; i++)
    {
        try
        {
            element = driver.findElement(By.cssSelector("html body div.navbar.navbar-inverse.main-navbar "));
            break;
        }
        catch (org.openqa.selenium.StaleElementReferenceException e)
        {
            System.out.println("Caught an Staleelement exception");
        }

        Thread.sleep(1000);
    }

    return element;
}
JeffC
  • 22,180
  • 5
  • 32
  • 55
Madis Kangro
  • 293
  • 3
  • 12
  • 2
    You are most probably catching a different `Exception` class... – Codebender Sep 21 '15 at 08:38
  • org.openqa.selenium.StaleElementReferenceException: {"message":"Element does not exist in cache"} Is the exception im getting when the test fails. – Madis Kangro Sep 21 '15 at 08:42
  • 2
    In that case, the exception is being thrown elsewhere.. [Check your stacktrace to pinpoint the location](http://stackoverflow.com/q/3988788/2775450) and debug it... – Codebender Sep 21 '15 at 08:47
  • Thanks for the help, your tips helped alot. I managed to find the issue. When I just tried to find the element this code worked fine. As soon as i did dropdown.click(); then it started getting staleelementreferences. – Madis Kangro Sep 21 '15 at 10:40

2 Answers2

2

I think there is more than one issue here.

First, I'd like you to give us your stacktrace. I assume, that the exception is thrown elsewhere and the stacktrace would tell us where.

Second, after you have written your "element" variable you call a break; statement, which will leave your loop, therefore no exception will be thrown. You can check it yourself. I assume that your for-loop will be exited while it is in its first iteration.

Therefore delete the break; statement.

Edit: If you are running a unit test I could bet that you caught the stalemateException in your actual method of your program (not the test). If the exception is caught there your testmethod will NOT receive it, for it is already caught. An easy attempt to see if that's the case is to throw your exception again after it is caught. For instance:

catch (org.openqa.selenium.StaleElementReferenceException  e) {

    throw e;
}

But don't throw your exception up to your Main class! Catch it anywhere, where the exception can be handled.

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
Tom Wellbrock
  • 2,982
  • 1
  • 13
  • 21
  • The thing is the code that I posted worked fine. (It breaks when i want it to break). The issue came from when I wanted to click the element. Seems like it wanted me to try and catch that also. Cheers I was able to debug the the issue and it works now. – Madis Kangro Sep 21 '15 at 10:38
0

Here's an alternative to your approach. You basically wait up to 60 seconds (customizable) for the element to become stale. I took a little out of your CSS selector since you (in general) don't really need to specify that the element is in the HTML and BODY tags.

public static WebElement DropDown(WebDriver driver)
{
    WebDriverWait wait = new WebDriverWait(driver, 60);
    WebElement element = driver.findElement(By.cssSelector("div.navbar.navbar-inverse.main-navbar"));
    wait.until(ExpectedConditions.stalenessOf(element));
    return element;
}
JeffC
  • 22,180
  • 5
  • 32
  • 55