Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
即使在android的appium中找不到或正在等待元素,如何继续_Android_Testing_Automation_Appium - Fatal编程技术网

即使在android的appium中找不到或正在等待元素,如何继续

即使在android的appium中找不到或正在等待元素,如何继续,android,testing,automation,appium,Android,Testing,Automation,Appium,我正在使用Appium(java)来自动化我的android应用程序。假设我试图找到一个按钮(按类/id/可访问性),但我没有得到它-他们要么服务器继续搜索特定id,要么抛出异常。使用try-catch是一个选项,但如果几秒钟后仍在搜索元素,该怎么办呢?请建议正确的方法继续创建一个方法,检查元素是否可用或是否使用您的尝试捕捉逻辑,例如 String elementID="something"; WebDriverWait wait = new WebDriverWait(driver, wait

我正在使用Appium(java)来自动化我的android应用程序。
假设我试图找到一个按钮(按类/id/可访问性),但我没有得到它-他们要么服务器继续搜索特定id,要么抛出异常。
使用
try-catch
是一个选项,但如果几秒钟后仍在搜索元素,该怎么办呢?
请建议正确的方法继续

创建一个方法,检查元素是否可用或是否使用您的尝试捕捉逻辑,例如

String elementID="something";
WebDriverWait wait = new WebDriverWait(driver, waitTime);
    try{
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(elementID)));
    }catch (Exception e) {
        log("INFO - "+elementID+" Element is Not Present");
        return false;
    }
return true;

如果方法返回true,则执行您的操作或断言它。

创建一个方法,该方法将使用您的try-catch逻辑检查元素是否可用,例如

String elementID="something";
WebDriverWait wait = new WebDriverWait(driver, waitTime);
    try{
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(elementID)));
    }catch (Exception e) {
        log("INFO - "+elementID+" Element is Not Present");
        return false;
    }
return true;

如果方法返回true,请执行您的操作或断言它。

尝试此操作。我将其包装为一个函数isElementPresent(),它是一个布尔值,如果元素存在,则返回true

public boolean isElementPresent(String identifier) throws IOException{

    MobileElement elementId = (MobileElement)driver.findElementById(identifier);

    Application_Log.info("Element found :- " , elementId);

    int totalSize = elementId.size();
    System.out.println(totalSize);

    if (totalSize!=0){

        System.out.println("Element found  : "+ elementId );

    }else{
        Application_Log.error("Element not found :- " + elementId);
        Assert.fail("Element not found :- " + elementId);
    }

    return false;
}

试试这个。我将其包装为一个函数isElementPresent(),它是一个布尔值,如果元素存在,则返回true

public boolean isElementPresent(String identifier) throws IOException{

    MobileElement elementId = (MobileElement)driver.findElementById(identifier);

    Application_Log.info("Element found :- " , elementId);

    int totalSize = elementId.size();
    System.out.println(totalSize);

    if (totalSize!=0){

        System.out.println("Element found  : "+ elementId );

    }else{
        Application_Log.error("Element not found :- " + elementId);
        Assert.fail("Element not found :- " + elementId);
    }

    return false;
}

以下是一个简单的解决方案:

 public static Boolean elementExists(By selector) {
    List<MobileElement> elementlist = driver.findElements(selector);
    return (!elementlist.isEmpty());
}
public静态布尔元素exists(通过选择器){
List elementlist=driver.findElements(选择器);
return(!elementlist.isEmpty());
}

重要的部分是使用driver.findElements,它将返回找到的元素列表。然后只需检查列表是否为空。

这里有一个简单的解决方案:

 public static Boolean elementExists(By selector) {
    List<MobileElement> elementlist = driver.findElements(selector);
    return (!elementlist.isEmpty());
}
public静态布尔元素exists(通过选择器){
List elementlist=driver.findElements(选择器);
return(!elementlist.isEmpty());
}

重要的部分是使用driver.findElements,它将返回找到的元素列表。然后只需检查列表是否为空。

WebElement feedsCatItems=(新WebDriverWait(driver,10))。直到(ExpectedConditions.presenceOfElementLocated(By.className(“android.view.view”))

你可以用这个。这将等待特定元素,然后移动。在这里,我正在等待视图,它等待了10秒。
这很简单。

WebElement feedsCatItems=(新的WebDriverWait(驱动程序,10))。直到(ExpectedConditions.presenceOfElementLocated(By.className(“android.view.view”))

你可以用这个。这将等待特定元素,然后移动。在这里,我正在等待视图,它等待了10秒。
这很容易。

谢谢。我将尝试它,并将其标记为答案,如果它有效,则在int totalSize=elementId.size()行中未定义类型MobileElement的方法size()。@AnswerDroid,将函数的第2行mobile element替换为此。List totalElement=driver.findelementsbyd((标识符))@答案是逻辑。试着用你需要的标识符。让我知道进展如何,汉克斯·卡迪克。我将尝试它,并将其标记为答案,如果它有效,则在int totalSize=elementId.size()行中未定义类型MobileElement的方法size()。@AnswerDroid,将函数的第2行mobile element替换为此。List totalElement=driver.findelementsbyd((标识符))@答案是逻辑。试着用你需要的标识符。让我知道进展情况NewCommand超时=60秒,60秒后驱动程序不会搜索NewCommand超时=60秒,60秒后驱动程序不会搜索