Search for element time

Hi! the code is searching infinity for an element that is not found and he’s not failing the test,
I’d like to know I can limit the time of searching elements

[debug] [W3C] Matched W3C error code 'no such element' to NoSuchElementError
[debug] [AndroidUiautomator2Driver@d3d6 (bca1e98a)] Waited for 27148 ms so far
[debug] [ADB] Checking app cert for C:\Users\wmejri\eclipse-workspace\cheqcorner_Appium_Tests\src\test\ressources\rc13.apk
[debug] [AndroidUiautomator2Driver@d3d6 (bca1e98a)] Matched '/element' to command name 'findElement'
[debug] [AndroidUiautomator2Driver@d3d6 (bca1e98a)] Proxying [POST /element] to [POST http://127.0.0.1:8202/session/ec04f843-7d07-4423-af34-b95de23294c9/element] with body: {"strategy":"id","selector":"fr.tessi.cheqcorner:id/deposit_stop_btn","context":"","multiple":false}
[AndroidUiautomator2Driver@d3d6 (bca1e98a)] Got response with status 404: {"sessionId":"ec04f843-7d07-4423-af34-b95de23294c9","value":{"error":"no such element","message":"An element could not be located on the page using the given search parameters","stacktrace":"io.appium.uiautomator2.common.exceptions.ElementNotFoundException: An element could not be located on the page using the given search parameters\n\tat io.appium.uiautomator2.handler.FindElement.safeHandle(FindElement.java:70)\n\tat io.appium.uiautomator2.handler.request.SafeRequestHandler.handle(SafeRequestHandler.java:59)\n\tat io.appium.uiautomator2.server.AppiumServlet.handleRequest(AppiumServlet.java:270)\n\tat io.appium.uiautomator2.server.AppiumServlet.handleHttpRequest(AppiumServlet.java:264)\n\tat io.appium.uiautomator2.http.ServerHandler.channelRead(ServerHandler.java:68)\n\tat io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:366)\n\tat io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:352)\n\tat io.netty.chann...`Preformatted text`
  1. public void waitForElementToAppear(WebElement elem, AppiumDriver driver) {
    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
    wait.until(ExpectedConditions.attributeContains(final WebElement element,
    final String attribute,
    final String value));
    }
  2. you can use implicit wait and explicit wait
1 Like

One question @Giuma how long it will take when you try to find your “elem” before starting “waitForElementToAppear” function?
if you want your function work you need use locator and limit search time inside function :slight_smile:

something like …

wait = new WebDriverWait(driver, Duration.ofSeconds(5));
wait.until(presenceOfElementLocated(MobileBy.AccessibilityId("login")))
1 Like

ow yes …
I wrote a generic function, where elem is a WebElement
where I specify the type of locator. this is my example, in my case i wanted it to wait until it found an element that contained a specific text.

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
wait.until(ExpectedConditions.attributeContains(
    driver.findElement(By.id("your id ")), "text", "the text of element"));
-attributeContains(webElement, string attribute, string value));

thanks you @Aleksei for being more specific :slight_smile:

1 Like