If else statement is not responding when condition provided in if(Condition) is false

Hi, i am using AppiumForWindows-, java-client-2.1.0 and Java with eclipse to do automation for a Android devices. I am trying to use if else statement to handle toggle of a button. As in the below code either of the two below id will we visible. But if the condition in the if statement is not true then it does not proceeds to the else statement.

System.out.println(“Sync is already running”);
}else {
WebElement sync = driver.findElement(By.id(“menu_refresh”));
System.out.println(“Sync Started”);


Hi @Saurav_Gayawar,

You can use a try…catch block in above scenario, adding else statements to a catch block.
Let me know if this does not help.


Thanks Sujata_Kalluri, try catch block worked.

The reason try catch works because that, findElement returns WebElement if the element exists in the application. So in your case WebElement.isDisplayed() returns true.
If the element doesnt exist in the application, then it throws NoSuchElementFoundException. So cannot validate this exception in if condition, where as we can do that in try catch.

Also, if someone does not want to use a catch block, they can user findElements that will return a List. Just check if it is empty when no element exists.

Even i am facing the same issue,but try catch is not working for me.
can anyone help me out??