如何在 Appium 测试中使用 if-else 语句 (Java)

How to use if-else statements in Appium Test (Java)

我想在 Appium 的测试中使用这样的结构:

if (element.exists()) {
 System.out.println("OK");
}

但是测试失败并出现 NoSuchElementException。 Thy/catch constructiont 也行不通。 如何在 Appium 中使用 if/else 语句?

您可以通过首先获取该元素的列表然后检查其大小来检查该元素是否存在。如果大小大于 0,则表示它存在于页面上,否则它不存在。
你可以这样做:

List<WebElement> elementList = driver.findElements(By.xpath("Enter your xpath here"));
if(elementList.size()>0){
   //Element is present
}
else{
   //Element is not present  
}

试试这个。

public boolean verifyElementIsVisible(){
MobileElement element=driver.findElement(By.id("#enter your id here");

      if(element.isVisible())
      {
         return true;
      }
      else
      {
         return false;
      }
}