AndroidDriver findElementByUiAutomator

Hi, I am using AndroidDriver findElementByAndroidUIAutomator() to find element in my app page. I wonder how I could check the existence of an element.
I was trying to achieve that by the following code:

      AndroidElement element = androiddriver.findElementByAndroidUIAutomator("finding string");
      if (element.isDisplayed()) {
          // find element 
      } else {
          // not find
      }

But the else condition was never reached when the actual element did not exist. The appium log is

Returning result: {“value”, “No element found”, "status’ : 7}
How can I get the message or exception of the element existence status like webelement? Thx in adavance.

Use try/catch block like this. You need to catch the specific nosuchelement exceptions. if else block cannot be used for that.

       try{
                     element.isDisplayed();
              // find element 
          } catch(NoSuchElementException e){
              // not find
          }
1 Like

on @hemche dont forget to put

AndroidElement element = androiddriver.findElementByAndroidUIAutomator("finding string");

inside the try catch block

1 Like

Nice. I did not realize of catching the exception…Thank you so much.

1 Like