a. wait for element for 3 sec with polling of 1 sec or less as required
b. if exception occured means element is not displayed in 3 sec
c. check if webelement is not null then only click it
try {
WebElement textView =
driver.findElement(By.id("com.simplemobiletools.gallery:id/text_view"));
new FluentWait<WebElement>(textView)
.withTimeout(3, TimeUnit.SECONDS)
.pollingEvery(1, TimeUnit.SECONDS)
.until(new Function<WebElement, Boolean>() {
@Override
public Boolean apply(WebElement element) {
return element.isDisplayed();
}
});
}
catch(TimeoutException e){
System.out.println("TimeoutException exception occured after 3 sec);
}
if (textView !=null){
// we do not bother about time here,
// just in case findElement returned something we can click here
driver.findElement(By.id("android:id/button2")).click();
}