If statement for clicking on an elememt



In one of my tests I have a pop-up that may or may not appear. I’d like to add an if statement to click if it pops up else carry on.

Currently I have got:

MobileElement ok = (MobileElement) driver.findElementsById(“elementhere”);
try {
if (ok.isEnabled()) ;
} catch (NoSuchElementException ignored) {

What this does though is just look for the element and try to click it. If it’s not there then it will fail. I guess I need to add an else statement in but not sure what it should contain.



Anyone able to help with the above?