使用MobileElement时显式等待不起作用



我正在使用appium来自动化android移动应用程序。我正在使用辅助功能id查找元素。当应用程序启动时,我想点击登录按钮。以下是我如何定位该元素的。

@AndroidFindBy(accessibility = "loginButton")
public MobileElement loginButton;

我有一个明确的等待方法如下:

public void waitUntilElementIsVisible(MobileElement id) {
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOf(id));
}

我这样称呼这种方法:

waitUntilElementIsVisible(loginLocators.loginButton).click();

但当测试开始时,它找不到登录按钮,并给了我"NoSuchElementError"。输出日志:

[HTTP] --> POST /wd/hub/session/ffa63718-deb5-4e15-8cd7-74363f2c084f/element
[HTTP] {"using":"accessibility id","value":"loginButton"}
[debug] [W3C (ffa63718)] Calling AppiumDriver.findElement() with args: ["accessibility id","loginButton","ffa63718-deb5-4e15-8cd7-74363f2c084f"]
[debug] [BaseDriver] Valid locator strategies for this request: xpath, id, class name, accessibility id, css selector, -android uiautomator
[debug] [BaseDriver] Waiting up to 0 ms for condition
[debug] [WD Proxy] Matched '/element' to command name 'findElement'
[debug] [WD Proxy] Proxying [POST /element] to [POST http://127.0.0.1:51197/wd/hub/session/5043b2ed-8ea9-4394-a074-353004a6a1f6/element] with body: {"strategy":"accessibility id","selector":"loginButton","context":"","multiple":false}
[WD Proxy] Got response with status 404: {"sessionId":"5043b2ed-8ea9-4394-a074-353004a6a1f6","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 parametersntat io.appium.uiautomator2.handler.FindElement.safeHandle(FindElement.java:70)ntat io.appium.uiautomator2.handler.request.SafeRequestHandler.handle(SafeRequestHandler.java:41)ntat io.appium.uiautomator2.server.AppiumServlet.handleRequest(AppiumServlet.java:261)ntat io.appium.uiautomator2.server.AppiumServlet.handleHttpRequest(AppiumServlet.java:255)ntat io.appium.uiautomator2.http.ServerHandler.channelRead(ServerHandler.java:68)ntat io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:366)ntat io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:352)ntat io.netty.chann...
[debug] [W3C] Matched W3C error code 'no such element' to NoSuchElementError
[debug] [W3C (ffa63718)] Encountered internal error running command: NoSuchElementError: An element could not be located on the page using the given search parameters.
[debug] [W3C (ffa63718)]     at AndroidUiautomator2Driver.findElOrEls (/usr/local/lib/node_modules/appium/node_modules/appium-android-driver/lib/commands/find.js:75:11)
[debug] [W3C (ffa63718)]     at processTicksAndRejections (internal/process/task_queues.js:97:5)
[HTTP] <-- POST /wd/hub/session/ffa63718-deb5-4e15-8cd7-74363f2c084f/element 404 29 ms - 460

它适用于隐式等待和thread.sleep((。但由于某些原因,我想使用显式等待。

有人能帮我解决这个问题吗?

尝试将方法的返回类型更改为WebElement:

public WebElement waitUntilElementIsVisible(MobileElement id) {
WebDriverWait wait = new WebDriverWait(driver, 10);
return wait.until(ExpectedConditions.visibilityOf(id));
}

最新更新