Govinda Rai
Govinda Rai

Reputation: 11

Loop within try catch where exception is thrown in condition of do while loop in selenium

I want to complete loop ignoring the exception.please suggest?

try {
do{
        driver.navigate().refresh();

                }while(!driver.findElement(By.partialLinkText("Completed")).isDisplayed());
            } catch (Exception e) {
                // TODO Auto-generated catch block

            }
}

Upvotes: 1

Views: 584

Answers (1)

Eran
Eran

Reputation: 393771

Put the try-catch inside the loop and use a variable to hold the result of the evaluation that you do in the condition :

boolean cont = false;
do {
    try {
        driver.navigate().refresh();
        cont = !driver.findElement(By.partialLinkText("Completed")).isDisplayed();
    } catch (Exception e) {
        cont = true; // if you want the loop to continue after an exception
                     // or false if you want to terminate the loop in this case
    }
} while (cont);

Upvotes: 4

Related Questions